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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:18:09+00:00 2026-05-31T13:18:09+00:00

The task is to read numbers from file to vector. Format is: one number

  • 0

The task is to read numbers from file to vector. Format is: one number per line. I want to make with STL-style. So, I wrote such code:

::std::deque<size_t> Input(const ::std::string& filename) {
    ::std::deque<size_t> result;
    try {
        ::std::ifstream file(filename.c_str(), ::std::ios::binary);
        file.exceptions(::std::ifstream::failbit |
                       ::std::ifstream::badbit);

        ::std::copy(::std::istreambuf_iterator<size_t>(file),
                    ::std::istreambuf_iterator<size_t>(),
                    ::std::back_inserter(result));

    } catch ( ::std::exception& e ) {
        ::std::cout << e.what() << ::std::endl;
    }
    return result;
}

It works fine, and I get what I want (all numbers from the file), but also I get an exception with failbit at end of file. What’s the problem? I don’t want to use getline() and parse by hand — wanna ask STL do it for me.
UPDATE: I checked — there is no new line at the end of file.

  • 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-31T13:18:10+00:00Added an answer on May 31, 2026 at 1:18 pm

    TL;DR: If the stream run out of content while trying to read a new value, failbit is set.


    It’s defined in the standard that using a istream_iterator has the same effect as doing:

    *in_stream >> value; /* istream_iterator::operator++ */
    return        value; /* istream_iterator::operator*  */
    

    Which in the case with handling integers (such as size_t) will results in calls to the below (can be read about under 27.6.1.2.2/2 – lib.istream.formatted.arithmetic).

    use_facet<numget> (loc).get (*this, 0, *this, err, lval)
    
    ...
    
    setstate (err);
    

    err is passed by reference and will be set to whatever errors the function might run into. Below is a snippet from the standards definition of use_facet<numget> (loc).get:

    22.2.2.1.2/8 - [lib.facet.num.get.virtuals]

    The in iterator is always left pointing one position beyond the last
    character successfully matched. If val is set, then err is set to
    str.goodbit; or to str.eofbit if, when seeking another character to
    match, it is found that (in == end).

    If val is not set, then err is
    set to str.failbit; or to (str.failbit|str.eofbit) if the reason for
    the failure was that (in == end).


    TL;DR: If the stream run out of content while trying to read a new value, failbit is set.

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

Sidebar

Related Questions

Here's a common, simple task: Read configuration settings from a configuration file, save the
Overview of My Situation: My task is to read strings from a file, and
I want to parse the text line from the Wavefront OBJ file . Currently
Simple task like make AJAX request , pass one parameter and return result, can
I'm trying to read a simple unicode (UTF-16) text file with just some numbers
I am trying to execute a certain task where i am required to read
Task: I have a camera mounted on the end of our assembly line, which
I am trying to run an Ant task from within IBM RSA IDE using
I'm C++ begginer. I did this excercise from Deitel's book: Use a one-dimensional array
I'm looking into developing an application that will process data from a line-scan camera

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.