Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read unsigned int variables from file correctly, using ifstream?

Tags:

c++

file

ifstream

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"?

like image 920
Lucky Man Avatar asked Jan 21 '12 12:01

Lucky Man


2 Answers

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.

like image 108
Dietmar Kühl Avatar answered Oct 13 '22 00:10

Dietmar Kühl


You can do it this way:

#include <iostream>
#include <locale>

class my_num_get : public std::num_get<char> {
protected:
    iter_type do_get(iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, unsigned int& v) const
    {
        in = std::num_get<char>::do_get(in, end, str, err, v);
        if(in != end && !std::isspace(*in, str.getloc()))
            err |= std::ios_base::failbit;
        return in;
    }
};

int main() {
    using namespace std;
    cin.imbue(std::locale(cin.getloc(), new my_num_get));
    cin.exceptions(ios_base::badbit | ios_base::failbit);
    try {
        unsigned int x;
        cin >> x;
    } catch(const std::exception &e) {
        cerr << e.what() << "\n";
    }
}

If you want this to work for other types too, then implement the following in the same way:

iter_type do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, T& v) const

where T is one of bool, long, long long, unsigned short, unsigned long, unsigned long long, float, double, long double and void*.

like image 30
Yakov Galka Avatar answered Oct 13 '22 00:10

Yakov Galka