为什么不工作?

时间:2010-12-31 14:30:11

标签: c# binary-data

BinaryReader br = new BinaryReader(Console.OpenStandardInput());
BinaryWriter bw = new BinaryWriter(Console.OpenStandardOutput());

int n = br.ReadInt32();
bw.Write(n);

总是收到此错误:

Unhandled Exception: System.IO.EndOfStreamException: Failed to read past end of stream.
at System.IO.BinaryReader.FillBuffer (Int32 numBytes) [0x00000] in <filename unknown>:0 
at System.IO.BinaryReader.ReadInt32 () [0x00000] in <filename unknown>:0 
at Program.Main () [0x00025] in /home/skydos/ACM/Csharp/Csharp/Main.cs:24 

有没有什么方法可以更快地从控制台中读取C#中的数据?

2 个答案:

答案 0 :(得分:4)

您是否正在为程序的标准输入写任何内容?错误说没有可用的数据,非常简单。

答案 1 :(得分:0)

MSDN文档中有很多关于如何处理System.IO内容的例子。也许这个链接会让你入门?

http://msdn.microsoft.com/en-us/library/ms404278.aspx?appId=Dev10IDEF1&l=EN-US&k=k(SYSTEM.IO.BINARYREADER);k(TargetFrameworkMoniker-%22.NETFRAMEWORK&k=VERSION=V4.0%22);k(DevLang-CSHARP)&rd=true

如果速度是您的主要关注点,那么Code Project中的这篇文章可能会给您一些想法:

http://www.codeproject.com/KB/files/fastbinaryfileinput.aspx