Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 272147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:16:17+00:00 2026-05-12T00:16:17+00:00

In VS 2005, I have some code that looks like this: ifs.open(foo); while (!ifs.eof())

  • 0

In VS 2005, I have some code that looks like this:

ifs.open("foo");
while (!ifs.eof())
{
    ifs.read(&bar,sizeof(bar));
    loc = ifs.tellg();
    loc += bar.dwHeaderSize;
    // four byte boundary padding
    if ((loc % 4) != 0)
        loc += 4 - (loc % 4);
    ifs.seekg(loc,ios::beg);
}
ifs.close();

The code worked fine in VS 2005, but it fails in VS 2008 Express. From what I can tell, VS 2008 is not returning eof() after the code seeks to the end of the file. Am I missing something? I fixed it by adding an explicit check to see if the seek location exceeded the file size, but I want to be sure I understand ifstream correctly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:16:18+00:00Added an answer on May 12, 2026 at 12:16 am

    The EOF flag is only triggered after you attempt to read past the end of file.
    Reading upto the end of file will not trigger it.

    This is why most code looks like this:

    while(ifs.read(&bar,sizeof(bar)))
    {
         // Do Stuff
    }
    

    If the result of the read() goes upto the EOF the loop will be entered.
    If the result of the read() goes past the EOF the loop will NOT be enetered

    • NOTE: read() will only go past the EOF if there are zero bytes left in the file. Otherwise it will read upto the end of the file. So The loop is always entered if there was somthing left in the file.

    The reason is the result of the read (the return value) is a reference to the stream. If the stream is used in a boolean context (such as the if test expression) it is converted into a type that can be used in such a context. The result of this conversion tests the EOF flag (in addition to a couple of others) and returns the quivalent of false if EOF is true.

    Note:
    This techniques works better if you overload the operator << for your Bar class as this should read in exactly what it needs for the object without going past the EOF. It is then easier to make your objects read exactly upto the end of the file without going over. The thing I worry about with read is what should happen if read() wants 10 bytesand there are only 5 bytes in the file, what happens with the partially filled object?

    If you want to continue using your style the code should look like this:

    ifs.open("foo");
    while (!ifs.eof())
    {
        ifs.read(&bar,sizeof(bar));
        if (ifs.eof())
        {    break;
        }
        // Do Stuff
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 231k
  • Answers 231k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the following function like this: Image('/path/to/original.image', '1/1', '150*', './thumb.jpg');… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer Check you database schema to see if the field (referenced… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer I figured out the problem - there was a session… May 13, 2026 at 2:13 am

Related Questions

I've been upgrading our solutions from VS 2005 to VS 2008; still targeting the
I have an old .net 1.0 webapp that needs a bit of maintenance done
I have an existing VS 2005 Std .NET Compact Framework application that I want
I have a solution containing several projects that have migrated from VS 2003, 2005,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.