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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:42:40+00:00 2026-06-15T03:42:40+00:00

Consider the following code: ifstream in; try { in.exceptions ( ifstream::failbit | ifstream::badbit );

  • 0

Consider the following code:

    ifstream in;
    try {
        in.exceptions ( ifstream::failbit | ifstream::badbit );
        in.open(pConfLocation);
    } catch ( ifstream::failure e ) {
        throw std::runtime_error("Can't open configuration file\n");
    }

    vector<string> lns;
    string s;

    in.clear();
    while ( !in.eof() ){
        getline( in, s );
        boost::algorithm::trim(s);
        lns.push_back( s+='\n');
    }

So:

  1. I set the following “exception mask” ( ifstream::failbit | ifstream::badbit ) for the needs of the try-catch block. The file opens without problems.
  2. In while{} block, I know that at the end of the file eofbit will be set. But

The exception mask is an internal value of all stream objects
specifying which state flags have to throw an exception when they are
set
.

I did NOT set ifstream::eofbit, but anyway the following error appears at runtime:

terminate called after throwing an instance of 'std::ios_base::failure'
  what():  basic_ios::clear
The program has unexpectedly finished.

I cannot understand this behavior. I tried to use in.clear() right before while{} but with no effect. clear() itself sets goodbit, and as far as I understand, “flags have to throw an exception” (see the quote above), but when googbit set it’s not causing to throw any exceptions…

If to delete

        in.exceptions ( ifstream::failbit | ifstream::badbit );

it works.


How to make getline() work in this case?

  • 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-06-15T03:42:42+00:00Added an answer on June 15, 2026 at 3:42 am

    The problem is in your input iteration. eofbit is only set when the last read reached EOF, not if the next read will read only EOF. When the latter happens, failbit is set at the same time. See the discussion here.

    In your particular case, if the file ends with a newline (as it probably does), the getline() reads up to and including that newline and returns. eofbit is still not set. The next getline() then encounters EOF directly, and as per its documentation, “If the function extracts no elements, it calls setstate(failbit).”

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

Sidebar

Related Questions

Consider the following code: #include <iostream> using namespace std; int main() { int x,
Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct
Consider the following sample code: #include <iostream> using namespace std; class base { public:
Consider the following pretty simple C++ code: #include <algorithm> #include <iostream> using namespace std;
Consider the following code: #include<iostream> #include<vector> using namespace std; class Foo { public: template<
Consider the following code: #include <iostream> using namespace std; class Test { static int
Consider the following code: #include <iostream> struct X{ X(){ throw 0; } }; void
Consider the following code: #include <iostream> using namespace std; class B{ public: B(){} };
Consider the following code. #include <iostream> #include <string> struct SimpleStruct { operator std::string ()
consider the following c++ code #include stdafx.h #include<iostream> using namespace std; this much part

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.