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

  • SEARCH
  • Home
  • 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 7031521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:48:50+00:00 2026-05-28T00:48:50+00:00

DISCLAIMER: Don’t use ::feof() as your loop condition. For example, see the answer to:

  • 0

DISCLAIMER: Don’t use ::feof() as your loop condition. For example, see the answer to: file reading: feof() for binary files

However, I have “real” code that demonstrates a problem that does not use ::feof() as my loop condition, but LOGICALLY, this is the easiest way to demonstrate the problem.

Consider the following: We iterate a character stream one-at-a-time:

FILE* my_file;
// ...open "my_file" for reading...

int c;
while(0 == ::feof(my_file))
{ // We are not at EOF
  c = ::getc(my_file);
  // ...process "c"
}

The above code works as expected: The file is processed one-char-at-a-time, and upon EOF, we drop out.

HOWEVER, the following has unexpected behavior:

FILE* my_file;
// ...open "my_file" for reading...

int c;
while(0 == ::_eof(::fileno(my_file)))
{ // We are not at EOF
  c = ::getc(my_file);
  // ...process "c"
}

I would have expected them to perform the same. ::fileno() properly returns the (integer) file descriptor every time. However, the test ::_eof(::fileno(my_file)) works exactly once, and then returns 1 (indicating an EOF) on the second attempt.

I do not understand this.

I suppose it is conceivable that ::feof() is “buffered” (so it works correctly) while ::_eof() is “un-buffered” and thinks the whole file is “read-in” already (because the whole file would have fit into the first block read in from disk). However, that can’t possibly be true given the purpose of those functions. So, I’m really at a loss.

What’s going on?

(Files are opened as “text”, are ASCII text files with about a dozen lines, MSVS2008, Win7/64.)

  • 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-28T00:48:51+00:00Added an answer on May 28, 2026 at 12:48 am

    I suppose it is conceivable that ::feof() is “buffered” (so it works
    correctly) while ::_eof() is “un-buffered” and thinks the whole file
    is “read-in” already (because the whole file would have fit into the
    first block read in from disk). However, that can’t possibly be true
    given the purpose of those functions. So, I’m really at a loss.

    I don’t know why you would think it “can’t possibly be true given the purpose of those functions.” The 2 functions are meant to operate on files that are opened and operated on in different ways, so they are not compatible.

    In fact, that is exactly what is happening. Try this:

    FILE* my_file;
    // ...open "my_file" for reading...
    
    int c;
    while(0 == ::_eof(::fileno(my_file)))
    { // We are not at EOF
      c = ::getc(my_file);
    
      long offset1 = ftell(my_file);
      long offset2 = _tell(fileno(my_file));
    
      if (offset1 != offset2)
      {
         //here you will see that the file pointers are different
         //which means that _eof and feof will fire true under different conditions
      }
      // ...process "c"
    }
    

    I will try to elaborate a bit based on your comment.

    When you call fopen, you are getting back a pointer to a file stream. The underlying stream object keeps it’s own file pointer which is separate from the actual file pointer associated with the underlying file descriptor.

    When you call _eof you are asking if you have reached the end of the actual file. When you call feof, you are asking if you have reached the end of the file stream. Since file streams are usually buffered, the end of the file is reached before the end of the stream.

    I’m still trying to understand your answer below, and what the purpose
    is for _eof() if it always returns 1 even when you didn’t read
    anything (after the first char).

    To answer this question, the purpose of _eof is to determine if you have reached the end of the file when using _open and _read to work directly with file descriptors, not when you use fopen and fread or getc to work with file streams.

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

Sidebar

Related Questions

Disclaimer Yes, I know: don't use PASSWORD(password) to encrypt passwords . I don't want
DISCLAIMER: The following code is not something I would ever use in a real
Disclaimer: I don't really know what I'm doing, so I may have phrased things
Disclaimer: This question is meant to be purely theoretical, so please don't ask me
Disclaimer: I don't have a whole ton of experience with Java Generics, but my
Disclaimer, I'm from a Java background. I don't do much C#. There's a great
Disclaimer: I don't actually know anything about nether Oracle nor Java. The issue is
Disclaimer: My posts are apparently always verbose. If you happen to know the answer
I have an Agree And Conditions file ( rtf document ) and/or a disclaimer
Disclaimer Please don't just vote to close this because the title looks subjective, and

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.