472,345 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,345 software developers and data experts.

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

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
2 22763
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stephen Ferg | last post by:
I use the newsgroup mainly via my Web browser and Google. Over the last few days, when I click on a link to a thread, I've been frequently...
2
by: Andrew | last post by:
VS.net Win 2k3 I'm getting the following error when attempting to debug. I'm not debugging remotely, just a local project. "Unable to start...
0
by: Peter O'Reilly | last post by:
I receive the following error when I post a webpage: "Unable to validate error" - System.Web.HTTPException. The web page is an ASP WebForm page. ...
0
by: Steve Chatham | last post by:
I am stuck on this. It ought to be a simple reason as to why this is problematic, in that it works on smaller groups of data (say under 40...
0
by: Victor Alcazar | last post by:
Our ASP.NET (VB) is generating sporadic "Unable to validate data" and "Invalid length for a Base-64 char array" errors. The production...
2
by: Harrie Verveer | last post by:
Hi, I'm building quiet a large webapplication (CMS) in ASP.NET. On localhost the application runs fine. For publishing we use 2 hosting providers...
0
by: brian.mills | last post by:
I'm having trouble with a web service I've been using since I've change computers. The project was in source safe, and i've pulled it out, but I can...
1
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi Trying to copy a complex data structure with this code public object Clone() { MemoryStream ms = new MemoryStream(); BinaryFormatter bf =...
0
by: Rocky4Q | last post by:
I wrote a small c# winform app to migrate some user info from one DB to another. The application works jsut fine but when I use the web application...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.