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