By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,898 Members | 1,602 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,898 IT Pros & Developers. It's quick & easy.

"Unable to read beyond the end of the stream." exception using BinaryReader class and ReadString method

P: n/a
I already found an alternative way to accomplish this (using
ReadBytes), still I'd like to understand why I'm getting and error
reading a text file using the following method. The exception is
returned on the ReadString call.

public string BinaryRead(string fileName)
{
FileStream stream = File.Open(file, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite);

BinaryReader reader = new BinaryReader(stream);
reader.BaseStream.Seek(0, SeekOrigin.Begin);

StringBuilder builder = new StringBuilder();

while(reader.PeekChar() > -1)
{
try
{
// The call to ReadString rises an exception.
builder.Append(reader.ReadString());
}
catch(Exception ex)
{
// Handle exception
}
}

return builder.ToString();
}
Bob Rock
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Bob,

I guess that the reader goes out of sync. Is the file written using the
BinaryWriter?
I'm asking because the BinaryReader expects strings that it reads with its
ReadString method to be written in special format. That's it expects to find
encoded length of the string prefixing the string characters. If you try to
read normal text files, they obviously don't satisfy its format
expectations. In this case it is most likely to get exception like that.

If this is the case use StreamReader instead.

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Bob Rock" <an***************@yahoo.com> wrote in message
news:98**************************@posting.google.c om...
I already found an alternative way to accomplish this (using
ReadBytes), still I'd like to understand why I'm getting and error
reading a text file using the following method. The exception is
returned on the ReadString call.

public string BinaryRead(string fileName)
{
FileStream stream = File.Open(file, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite);

BinaryReader reader = new BinaryReader(stream);
reader.BaseStream.Seek(0, SeekOrigin.Begin);

StringBuilder builder = new StringBuilder();

while(reader.PeekChar() > -1)
{
try
{
// The call to ReadString rises an exception.
builder.Append(reader.ReadString());
}
catch(Exception ex)
{
// Handle exception
}
}

return builder.ToString();
}
Bob Rock

Nov 16 '05 #2

P: n/a
Bob Rock <an***************@yahoo.com> wrote:
I already found an alternative way to accomplish this (using
ReadBytes), still I'd like to understand why I'm getting and error
reading a text file using the following method. The exception is
returned on the ReadString call.


ReadString tries to read a length-prefixed string - were the strings
written by BinaryWriter?

If you're trying to just read a text file, I suggest you use
StreamReader instead and ReadLine or ReadToEnd.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.