Solved

Detect end of file

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

17 Experts available now in Live!

Get 1:1 Help Now