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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:52:35+00:00 2026-06-09T02:52:35+00:00

std::getline throws exception when it gets an eof . this is how I am

  • 0

std::getline throws exception when it gets an eof.
this is how I am doing.

std::ifstream stream;
stream.exceptions(std::ifstream::failbit|std::ifstream::badbit);
try{
  stream.open(_file.c_str(), std::ios_base::in);
}catch(std::ifstream::failure e){
  std::cout << "Failed to open file " << _file.c_str() << " for reading" << std::endl;
}
while(!stream.eof()){
  std::string buffer = "";
  std::getline(stream, buffer);
  //process buffer
  //I do also need to maintain state while parsing
}

In the above code getline is throwing exception as it gets eof
How to handle this situation ?

EDIT

std::string buffer = "";
while(std::getline(stream, buffer)){
    //also causes getline to hit eof and throw
}
  • 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-09T02:52:36+00:00Added an answer on June 9, 2026 at 2:52 am

    You activate the exception handling of your stream at the very beginning of your code:

    stream.exceptions(std::ifstream::failbit|std::ifstream::badbit);
    

    Now if the extraction of formatted data such as floating-point values, integers or strings will fail, it will set the failbit:

    eofbit    indicates that an input operation reached the end of an 
              input sequence;
    failbit   indicates that an input operation failed to read the expected 
              characters, or that an output operation failed to generate the 
              desired characters.

    While getline(stream,buffer) will indeed set the eofbit if it reaches the end of a file, it will also set the failbit, since the desired characters (a line) couldn’t be extracted.

    Either wrap another try-catch-block around your loop or disable the failbit exception.

    Example:

    #include <iostream>
    #include <fstream>
    
    int main(){
      std::ifstream stream("so.cc");
      stream.exceptions(std::ifstream::failbit|std::ifstream::badbit);
      std::string str;
    
      try{
        while(std::getline(stream, str));
      }catch(std::ifstream::failure e){
        std::cerr << "Exception happened: " << e.what() << "\n"
          << "Error bits are: "
          << "\nfailbit: " << stream.fail() 
          << "\neofbit: " << stream.eof()
          << "\nbadbit: " << stream.bad() << std::endl;    
      }
      return 0;
    }
    

    Result:

    Exception happened: basic_ios::clear
    Error bits are:
    failbit: 1
    eofbit: 1
    badbit: 0

    Note that both eofbit and failbit are set.

    See also:

    • std::ios_base::iostate
    • Table 124 in § 27.5.3.1.5 (see above)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

std::ifstream sr(path.c_str()); if (!sr) throw runtime_error(Could not open file '+path+\'); sr.seekg(0, ios::end); streampos lastPos
I would like to read some data from a stream I have using std::getline
In a C++ program, using std::ifstream, I'm attempting to open a user-specified file --
I have been following this convention thus far: std::string line; while(std::getline(in,line)) { if(line.size() &&
int main() { std::deque<std::string> mydeque; std::back_insert_iterator<decltype(mydeque)> myback_insert_iterator(mydeque); std::ifstream myifstream(test.txt); while(std::getline(myifstream, *myback_insert_iterator)) { } }
I want to read from a stream using std::getline inside a for loop. The
I'm using std::getline() to read lines from an std::istream-derived class, how can I move
std::vector<bool> reprVectors::encode(std::vector<float> input){ std::vector<float> distance; for(size_t i=0;i<this->reprVectorsList.size();i++){ distance.push_back(distBtw(input,this->reprVectorsList[i])); } std::vector<float>::iterator it= min_element(distance.begin(),distance.end()); return this->reprVectorsList[it]->code;
std::string x(x); This crashes very badly on my compiler. Does this mean I should
I have this code, int main() { std::string st; std::stringstream ss; ss<<hej hej med

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.