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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:20:05+00:00 2026-06-05T01:20:05+00:00

istream &func(istream &in) { string data; while (in >> data, !in.eof()) { if (in.bad())

  • 0
istream &func(istream &in)
{
    string data;

    while (in >> data, !in.eof()) {
        if (in.bad())
            throw runtime_error("IO stream corrupted");
        if (in.fail()) {
            cerr << "bad data, try again" << endl;
            in.clear();
            in.ignore(200);
            continue;
        }   

        cout << data << endl;;
    }   

    //in.clear(istream::eofbit | istream::failbit);
    in.clear();

    return in; 
}

why in.clear(istream::eofbit | istream::failbit); can not reset the cin?

but in.clear can make it.

PS: I use this function in main(), and use cin as its parameter.

  • 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-05T01:20:06+00:00Added an answer on June 5, 2026 at 1:20 am

    clear is defined like this:

    void clear(iostate state = goodbit);

    So, effectively, in.clear(); is doing this: in.clear(istream::goodbit); which resets the stream. Calling in.clear(istream::eofbit | istream::failbit); would set both the eofbit and failbit, I doubt that is what you want.

    std::cin.clear(std::istream::eofbit | std::istream::failbit);
    std::ios_base::iostate state = std::cin.rdstate();
    if( state == (std::istream::eofbit | std::istream::failbit))
    {
        std::cout << "eofbit and failbit set" << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Specifically I'm interested in istream& getline ( istream& is, string& str ); . Is
I though this would work since ifstream inherits from istream string getFileContents(istream& file_contents) {
When I try to compile the code istream in; if (argc==1) in=cin; else {
#include <iostream> #include <string> using namespace std; void printstr( const string & s )
I have a function which takes as argument an std::istream& and writes a transformed
I have this function declaration and definition.. definition void loadFromFile( string const& fileName, Frames&
std::istream has the prototype istream& read (char* s, streamsize n) the actual number of
template<typename T> std::istream & read(std::istream & istr, typename std::enable_if<std::is_pod<T>::value, T>::type & value) { return
void foo(ifstream &ifs) { //do something } int main() { fstream fs(a.txt, fstream::in); foo(fs);
I am trying to do the following: template <class T> std::ifstream& operator>> (std::ifstream& fin,

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.