Solved

Detect end of file

Posted on 2011-09-24
4
268 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

832 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