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

The Archive Base Latest Questions

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

bool ios::eof ( ) const; According to the library, The function returns true if

  • 0
bool ios::eof ( ) const;

According to the library,

The function returns true if the eofbit stream’s error flag has been
set by a previous i/o operation. This flag is set by all standard
input operations when the End Of File is reached in the sequence
associated with the stream.

I wrote a program to run some tests:

int main(int argc, char *argv[])
{
    ifstream ifs(argv[1]);
    float f;
    ifs >> f;

    cout << ifs.eof() << endl; //check if eofbit set

    ifs.close();
}

I tested 2 files, testcase1.txt and testcase2.txt.

testcase1.txt was generated in the terminal with cat, [Ctrl-D] was used to end input:

[~/C++ $] cat > testcase1.txt
1.234[Ctrl-D]

testcase2.txt was generated in vim, I opened up vim and just inputted 1.234, and then saved and exited.

Test Result

Test result with testcase1.txt is 1, which means the eofbit is set,

[~/C++ $] ./a.out testcase1.txt
1

Test result with testcase2.txt is 0,

[~/C++ $] ./a.out testcase2.txt
0

I open both testcase1.txt and testcase2.txt in vim, they look exactly the same, then why the eofbit wasn’t set for testcase2.txt?

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

    As you see in comment, there is a new line:

    ifstream ifs(argv[1]);
    float f;
    ifs >> f;
    char c;
    ifs.get( c ); // this will get '\n'
    

    Even so, the EOF still won’t set…. Read the paragraph you quoted again:

    The function returns true if the eofbit stream’s error flag has been
    set by a previous i/o operation. This flag is set by all standard
    input operations when the End Of File is reached in the sequence
    associated with the stream.

    To get the eof bit set, you have to read PASS the eof. You can use peek() to do it if you want.

    ifstream ifs(argv[1]);
    float f;
    ifs >> f;
    char c;
    ifs.get( c ); // this will get '\n'
    ifs.eof();  // this is false;
    ifs.peek();
    ifs.eof(); // this is true
    

    See also: istream::peek curious behavior wrt. EOF

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

Sidebar

Related Questions

Has something changed recently in iOS? I've been able to call the following in
According to iOS's Using and Creating Error Objects , one can display a error
bool operator()(Iterator it1, Iterator it2) const { return (*it1 < *it2); } Can someone
class Foo { static bool Bar(Stream^ stream); }; class FooWrapper { bool Bar(LPCWSTR szUnicodeString)
class Test { bool isVal() const { return isVal; } private: bool isVal; };
I am writing an iOS app that uses live audio analysis. It has an
There's this method in UIColor in iOS 5: - (BOOL)getHue:(CGFloat *)hue saturation:(CGFloat *)saturation brightness:(CGFloat
I have an iOS app built since the beginning with an error in it.
i have a function like this: BOOL WINAPI MyFunction(HDC hdc, LPCWSTR text, UINT cbCount){
I am working in a UIWebView ios app. in the delegate method: - (BOOL)webView:(UIWebView

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.