0

Im doing a school work and im getting this error. How could i fix it? Code and error bellow.

Error:

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

Code:

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();
    ```
  • That's because you're using `ReadString()` and others without checking if you're not already at the end of the file – Cid May 18 '20 at 11:05
  • It seems that you check that there is one char more in file, but try to read a lot of more. Check the file. – Guru Stron May 18 '20 at 11:17

0 Answers0