Good day to all, faced with a problem

Reading after the end of the stream is not possible.

The fact is that I am reading a binary file:

while (reader2.PeekChar() > -1) { numb2 = reader2.ReadInt32(); Int32 ndef2 = reader2.ReadInt32(); BitVector32 bv2 = new BitVector32(ndef); int nr2 = bv2[nr_s]; y2 = bv2[y_s]; int sm2 = bv2[sm_s]; ds2 = bv2[d_s]; ms2 = bv2[m_s]; int rs2 = bv2[r_s]; } 

And it so happened that numb2 has an entry in the file therefore reader2.PeekChar() > -1 greater than 1, but ndef2 is no longer in the file, and I get a read error after the end of the stream, maybe there is a more correct method for initializing data in the file, and if there is no further record, then just pass 0 and go on, or skip and go on, just do not want to do something like:

  while (reader2.PeekChar() > -1) { numb2 = reader2.ReadInt32(); if (reader2.PeekChar() > -1){ Int32 ndef2 = reader2.ReadInt32(); BitVector32 bv2 = new BitVector32(ndef); int nr2 = bv2[nr_s]; y2 = bv2[y_s]; int sm2 = bv2[sm_s]; ds2 = bv2[d_s]; ms2 = bv2[m_s]; int rs2 = bv2[r_s]; } } 

Or will you still have to put another check, as I wrote above?
Please help me figure it out.

  • It is already late evening, it is possible that I suggest stupidity, but nevertheless - As I understand from your words, you have determined that the file has missing data. You know the length of the data block readable in one iteration, why not find out the length of the file in bytes before the loop, divide it by the block length modulo, and if there is a remainder other than zero, instead of performing the last iteration, handle the error as you need. - Alexander Muksimov
  • numb2 Yes, I could do this, for example, put the number of bytes in the buffer and then read them through Read , but my file is appended every time and I cannot know its exact length, I can only know that there are numb2 values ​​and everything and ndef2 may ndef2 may not have information. - Ethernets
  • @Alexander Muksimov Perhaps I didn’t fully understand you, and I didn’t fully understand .... - Ethernets
  • Correctly, do I understand that you are simultaneously writing and reading from the same file? - Alexander Muksimov
  • @Alexander Muksimov Yes, one program writes, the second one I am writing reads, and all this happens at the same time. - Ethernets

0