首页 > 解决方案 > 错误 - 无法读取超出流末尾的内容

问题描述

我在做功课,我收到了这个错误。我该如何解决?代码和错误如下。

错误:

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();
    ```

标签: c#

解决方案


推荐阅读