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 250749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:32:13+00:00 2026-05-11T21:32:13+00:00

I recently ran into a problem caused by using fstream::eof(). I read the following

  • 0

I recently ran into a problem caused by using fstream::eof(). I read the following line from here:

The function eof() returns true if the end of the associated input file has been reached, false otherwise.

and (mistakenly) assumed this meant that if I used fstream::read() and read past the end of the file, the function eof() would tell me. So I did something like this (very generalized):

for(int i = 0; i < max && !file.eof(); i++)
{
     file.read(mything, sizeof(mything));
}

The problem came because of what is explained later on the page linked above (which I failed to read initially, thanks to the misleading first paragraph):

Conversely, the stream does not go into EOF state if there happens to be any whitespace after the last token, but trying to read another token will still fail. Therefore, the EOF flag cannot be used as a test in a loop intended to read all stream contents until EOF. Instead, one should check for the fail condition after an attempt to read.

So I changed, and now my loop checks against file.fail() rather than file.eof(), and I understand HOW eof() works. My question is, why does it work that way? Are there situations where this is desirable? It seems to me that once you’ve passed EOF, you’ve passed EOF and eof() should return true.

UPDATE
Thanks for the responses, I think I’ve got it. The only operation I’m performing is read(), and I immediately check for fail(), so I think I’m okay. Now, my question is, what would I use eof() for?

  • 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-11T21:32:14+00:00Added an answer on May 11, 2026 at 9:32 pm

    Because this way it can detect EOF without knowing how large the file is. All it has to do is simply attempt to read and if the read is short (but not an error), then you have reached the end of the file.

    This mirrors the functionality of the read system call, which file IO typically ends up calling (win32 stuff may call ReadFile but I believe that the functionality is similar).

    From the read manpage “RETURN VALUE” section (emphasis added):

    On success, the number of bytes read
    is returned (zero indicates end of
    file)
    , and the file position is
    advanced by this number. It is not an
    error if this number is smaller than
    the number of bytes requested; this
    may happen for example because fewer
    bytes are actually available right now
    (maybe because we were close to
    end-of-file, or because we are reading
    from a pipe, or from a terminal), or
    because read() was interrupted by a
    signal. On error, -1 is returned, and
    errno is set appropriately. In this
    case it is left unspecified whether
    the file position (if any) changes.

    BTW: a good way to write what you wanted would be like this:

    T something;
    while(file.read(something, sizeof(something))) {
        // process your 'something'
    }
    

    this works because file.read (like many members of iostream) return a reference to the iostream itself. All of which have an operator overloaded to allow testing the stream state. Similarly to read from std::cin, while(std::cin >> x) { ... } works as well.

    EDIT: you should know that testing vs. fail can be equally wrong for the same reason. From the page you linked to fail() returns if the previous operation failed. Which means you need to perform a read or other relevant operation before testing it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently ran into a bug in my code when using boost::bind . From
I borrowed the MyType trick from Landei here . But recently I ran into
I recently ran into a problem with a COM object that was using a
I'm writing a C parser using PLY, and recently ran into a problem. This
Recently I ran into a problem: Valid JSON when passed to the eval() function
Here's a problem I ran into recently. I have attributes strings of the form
A problem I recently ran into was that when trying to update a field
Interesting problem I ran into recently: I implemented a Stream class (a wrapping stream
I recently ran into a quite complex problem and after looking around a lot
I recently started programming my first Cocoa app. I have ran into a problem

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.