Solved

Detect end of file

Posted on 2011-09-24
4
264 Views
Last Modified: 2012-05-12
I currently have:
try
{
  for (; ; )
  {
    y = r.ReadByte();
  }
}
catch (EndOfStreamException)
{
  //end of file
}

Open in new window

Is there a better way? I feel I shouldn't be using catch for an expected exception.
0
Comment
Question by:deleyd
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 334 total points
ID: 36592916
r looks like a BinaryReader. Could we see the call to its constructor so that we can know what is the underlying stream? Some streams, such as the FileStream, have a marker for the end of the stream. You could be able to catch the condition through the BaseStream.
0
 

Author Comment

by:deleyd
ID: 36593733
using (FileStream fs = new FileStream(fn, FileMode.Open, FileAccess.Read))
{
  using (BinaryReader r = new BinaryReader(fs))
  {
    try
    {
      for (; ; )
      {
        y = r.ReadByte();
      }
    }
    catch (EndOfStreamException)
    {
      //end of file
    }

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 166 total points
ID: 36593992
How about modding line 7 to be:

for ( ; fs.Position < fs.Length; )

Open in new window

0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 334 total points
ID: 36594089
As I expected. You cannot know where the cursor is in the BinaryStream itself, but you can in the underlying FileStream.

     while (! (fs.Position==fs.Length))
      {
        y = r.ReadByte();
      }

Open in new window


You might have to adjust fs.Length plus 1 or minus 1, I do not remember if Position starts at index 0 or 1.
                        {}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now