c# - 错误 - 无法读取超出流末尾的内容
问题描述
我在做功课,我收到了这个错误。我该如何解决?代码和错误如下。
错误:
Unhandled exception. System.IO.EndOfStreamException: Unable to read beyond the end of the stream.
at System.IO.BinaryReader.ReadByte()
at System.IO.BinaryReader.Read7BitEncodedInt()
at System.IO.BinaryReader.ReadString()
at ProjetofinalMOD6E7.Program.Main(String[] args) in C:\Users\Wyzo\Desktop\Escola\ProjetofinalMOD6E7\ProjetofinalMOD6E7\Program.cs:line 78
代码:
List<Encomenda> ListaEncomenda = new List<Encomenda>();
BinaryReader txtIn = new BinaryReader(new FileStream(".\\encomendas.dat", FileMode.OpenOrCreate, FileAccess.Read));
while ((txtIn.PeekChar() != -1))
{
string Produto = txtIn.ReadString();
string NomeCliente = txtIn.ReadString();
string CidadeDestina = txtIn.ReadString();
int Quantia = txtIn.ReadInt32();
double Preco = txtIn.ReadDouble();
ListaEncomenda.Add(new Encomenda(Produto, NomeCliente, CidadeDestina, Quantia, Preco, DateTime.Now));
}
txtIn.Close();
```
解决方案
推荐阅读
- java - 有没有办法防止使用 CHECKSTYLE:OFF
- sum - 在给定条件下最大化数组和模M
- typescript - TypeScript 在类型构造函数中推断回调返回类型
- android - 当我启动应用程序时,回收视图不显示项目,但在转到另一个片段并返回后它可以工作
- python - OpenCV: Merging fitted shapes
- google-maps - (地图)当我添加新 Pin 图时,避免相机返回到之前的位置
- java - 在地图集合中添加多个值的有效方法
- visual-studio - 如何在 Visual Studio 中根据 OutputType 选择不同的规则集?
- css - CSS - 引导带禁用/全部删除选择器 (*)
- extjs - ExtJS Modern——向网格单元添加按钮