473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 23429
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 getting the message "Unable to find thread." Does...
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 debugging. Cannot find port. Check the remote...
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. I have a few controls on the page that when...
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 records), but doesn't on larger groups of records (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 environment is two Windows 2000 Advanced Servers in a web...
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 at the moment (let's call those 'A' and 'B')....
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 now not change beetween static and dynamic on a...
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 = new BinaryFormatter(); bf.Serialize(ms, this);...
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 now it bombs trying to read the users password with...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.