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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:57:59+00:00 2026-06-18T06:57:59+00:00

Let’s say we have a stream containing simply: hello Note that there’s no extra

  • 0

Let’s say we have a stream containing simply:

hello

Note that there’s no extra \n at the end like there often is in a text file. Now, the following simple code shows that the eof bit is set on the stream after extracting a single std::string.

int main(int argc, const char* argv[])
{
  std::stringstream ss("hello");
  std::string result;
  ss >> result;
  std::cout << ss.eof() << std::endl; // Outputs 1
  return 0;
}

However, I can’t see why this would happen according to the standard (I’m reading C++11 – ISO/IEC 14882:2011(E)). operator>>(basic_stream<...>&, basic_string<...>&) is defined as behaving like a formatted input function. This means it constructs a sentry object which proceeds to eat away whitespace characters. In this example, there are none, so the sentry construction completes with no problems. When converted to a bool, the sentry object gives true, so the extractor continues to get on with the actual extraction of the string.

The extraction is then defined as:

Characters are extracted and appended until any of the following occurs:

  • n characters are stored;
  • end-of-file occurs on the input sequence;
  • isspace(c,is.getloc()) is true for the next available input character c.

After the last character (if any) is extracted, is.width(0) is called and the sentry object k is destroyed.
If the function extracts no characters, it calls is.setstate(ios::failbit), which may throw ios_base::failure (27.5.5.4).

Nothing here actually causes the eof bit to be set. Yes, extraction stops if it hits the end-of-file, but it doesn’t set the bit. In fact, the eof bit should only be set if we do another ss >> result;, because when the sentry attempts to gobble up whitespace, the following situation will occur:

If is.rdbuf()->sbumpc() or is.rdbuf()->sgetc() returns traits::eof(), the function calls setstate(failbit | eofbit)

However, this is definitely not happening yet because the failbit isn’t being set.

The consequence of the eof bit being set is that the only reason the evil-idiom while (!stream.eof()) doesn’t work when reading files is because of the extra \n at the end and not because the eof bit isn’t yet set. My compiler is happily setting the eof bit when the extraction stops at the end of file.

So should this be happening? Or did the standard mean to say that setstate(eofbit) should occur?


To make it easier, the relevant sections of the standard are:

  • 21.4.8.9 Inserters and extractors [string.io]
  • 27.7.2.2 Formatted input functions [istream.formatted]
  • 27.7.2.1.3 Class basic_istream::sentry [istream::sentry]
  • 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-18T06:58:00+00:00Added an answer on June 18, 2026 at 6:58 am

    std::stringstream is a basic_istream and the operator>> of std::string “extracts” characters from it (as you found out).

    27.7.2.1 Class template basic_istream

    2 If rdbuf()->sbumpc() or rdbuf()->sgetc() returns traits::eof(), then the input function, except as
    explicitly noted otherwise, completes its actions and does setstate(eofbit), which may throw ios_-
    base::failure (27.5.5.4), before returning.

    Also, “extracting” means calling these two functions.

    3 Two groups of member function signatures share common properties: the formatted input functions (or
    extractors) and the unformatted input functions. Both groups of input functions are described as if they
    obtain (or extract) input characters by calling rdbuf()->sbumpc() or rdbuf()->sgetc(). They may use
    other public members of istream.

    So eof must be set.

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

Sidebar

Related Questions

Let's say I have an abstract parent class called shape, and that there are
Let's say that I have classes like this: public class Parent { public int
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say I have two text files that I need to extract data out
Let's say I have pseudocode like this: main() { BOOL b = get_bool_from_environment(); //get
Let's say for a moment that I have the following module in python: class
Let's say I have an actor like this (Using Akka 2.1 with Scala 2.10):

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.