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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:35:10+00:00 2026-05-28T17:35:10+00:00

My code reads unsigned int variables from the text file Input_File_Name . unsigned int

  • 0

My code reads unsigned int variables from the text file Input_File_Name.

unsigned int Column_Count; //Cols
unsigned int Row_Count;//Rows

try {
    ifstream input_stream;
    input_stream.open(Input_File_Name,ios_base::in);
    if (input_stream) {
        //if file is opened
        input_stream.exceptions(ios::badbit | ios::failbit);
        input_stream>>Row_Count;
        input_stream>>Column_Count;


    } else {
        throw std::ios::failure("Can't open input file");
        //cout << "Error: Can't open input file" << endl;
    }

} catch (const ios::failure& error) {
    cout << "Oh No!!" << error.what() << endl;          
} catch (const exception& error) {
    cout << error.what() <<"Oh No!!" << endl;
} catch (...) {
    cout << "Unknown exception" << endl;
}

It works excellent.
But when I fill text file with a wrong data

33abcd4  567fg8

It works in such way:

input_stream>>Row_Count; //Row_Count = 33;
input_stream>>Column_Count; // throws an ios::failure exception

Why doesn’t this line input_stream>>Row_Count; throw exception?
As I understood, input_stream considers any non-numeric symbol as delimiter, and on the next step it tries to read “abcd”. Is it so?
How to set a Space-symbol as delimiter to throw an ios::failure exception from this line of code input_stream>>Row_Count; while reading “33abcd4”?

  • 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-28T17:35:10+00:00Added an answer on May 28, 2026 at 5:35 pm

    The normal extraction of an integer value succeeds if the stream could read any integer value. That is, if there is at least one digit optionally followed by anything the read of an integer succeeds. The normal extraction operations don’t try to read more, in particular they don’t try to find the next whitespace.

    From the sounds of it, you want to be sure that there is a whitespace following your number and fail if there is not. I can think of two different approaches to do this:

    1. Create a simple manipulator which checks for the stream being on a whitespace character. This, however, means that you would read your values using something like in >> value >> is_space.
    2. Create a custom std::num_get<char> facet, install it into a std::locale, and imbue() this std::locale into your stream(s). It is a bit more involved but doesn’t require any changes to the way integers are read.

    Creating a manipulator like this is fairly trivial:

    std::istream& is_space(std::istream& in)
    {
        if (!std::isspace(in.peek()))
        {
            in.setstate(std::ios_base::failbit);
        }
        return in;
    }
    

    Now, changing the way numbers are read is more interesting and I suspect I had just named a number of standard library classes most people are fairly unaware of. So, let’s quickly type out an example for this as well. I will change the std::num_get<char> facet only to deal with unsigned int: to do it for other integral types it is necessary to override more functions. So, here is a replacement for the std::num_get<char> facet:

    class num_get:
        public std::num_get<char>
    {
        iter_type do_get(iter_type it, iter_type end,
                         std::ios_base& ios, std::ios_base::iostate& err,
                         unsigned int& value) const
        {
            it = std::num_get<char>::do_get(it, end, ios, err, value);
            if (it != end && !isspace(static_cast<unsigned char>(*it)))
            {
                err |= std::ios_base::failbit;
            }
            return it;
        }
    };
    

    All this does is to derive a class from std::num_get<char> and override one of its virtual functions. The implementation of this function is fairly straight forward: start with reading the value by delegating to the base class (I just realized that virtual functions indeed want to protected rather than private as I have though in the past but this is an entirely different discussion). Independent of whether this was successful (if it was not, it will have set up an error state in err) the override checks if there is another character available and, if so, checks if it is a space and if not sets a std::ios_base::failbit in the error result err.

    What remains is to set up the stream to use this particular facet in a std::locale and hook the new std::locale into a stream:

    std::locale loc(std::locale(), new num_get);
    in.imbue(loc);
    

    The std::locales and its facets are internally reference counted, i.e. you shouldn’t keep track of the pointer to the facet and you don’t need to keep the std::locale around either. If it seems to be to cumbersome to imbue() the created std::locale or you want to use this modified logic everywhere, you can set the global std::locale which is used to initialize any newly created stream to use the custom std::num_get<char> facet.

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

Sidebar

Related Questions

We have some C# code that reads data from a text file using a
The following code reads a text file one character at the time and print
I'm writing some quick code to try and extract data from an mp3 file
This is the code: unsigned int number; FILE* urandom = fopen(/dev/urandom, r); if (urandom)
Our Java code (not the test code) reads files from the current directory, which
I have the following code which reads in the follow file, append a \r\n
In my .aspx.cs I have a code that reads a .xml file and I
I developed my own Matrix class. Constructor reads a matrix from file. Matrix has
I'm writing a program that reads a text file of city names into a
I have the following C code: static void* heap; static unsigned int ptr; int

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.