Im getting error Unable to read beyond the end of the stream why?

前端 未结 3 1881
再見小時候
再見小時候 2021-01-18 14:13

The testing.res file is 240MB size. I want to read it. But im getting the error on this line:

int v = br.ReadInt32();

EndOfStreamException

3条回答
  •  后悔当初
    2021-01-18 14:27

    One reason your code could fail is if file contains extra bytes (i.e. 7 byte long file). Your code will trip on last 3 bytes.

    To fix - consider computing number of integers in advance and using for to read:

    var count = br.BaseStream.Length / sizeof(int);
    for (var i = 0; i < count; i++)
    {
      int v = br.ReadInt32();
      textBox1.Text = v.ToString();
    }
    

    Note that this code will simply ignore last 1-3 bytes if they are there.

提交回复
热议问题