4

I have code that parses a configuration file, which may send output to stdout or stderr in case of errors.

Unfortunately, when I pipe the output of my program to /dev/null, I get an exception: ios_base::clear: unspecified iostream_category error with stderror Inappropriate ioctl for device.

Here is the relevant part of my code:

try {
    file.exceptions(std::ios::failbit | std::ios::badbit);
    file.open(config_file);
    // file.exceptions(std::ios::failbit);
}
catch (std::ios_base::failure& e) {
    throw std::invalid_argument(std::string("Can't open config file ") + config_file + ": " + strerror(errno));
}
try {
    errno = 0; // reset errno before I/O operation.
    // ...
    while (std::getline(file, line)) {
        if ( [... unknown line ...] ) {
            std::cerr << "Invalid line in config " << config_file << ": " << line << std::endl;
            continue;
        }

        // debug info to STDOUT:
        std::cout << config_file << ": " << line << std::endl;

    }
} catch (std::ios_base::failure& err) {
    std::cout << "Caught exception " << err.what() << std::endl;
    if (errno != 0) {
        char* theerror = strerror(errno);
        file.close();
        throw std::invalid_argument(std::string("Can't read config file ") + config_file + ": " + theerror);
    }
}
try {
    file.close();
}
catch (std::ios_base::failure& e) {
    throw std::invalid_argument(std::string("Can't close config file ") + config_file + ": " + strerror(errno));
}

Here is an example of an exception:

~> ./Application test1.conf > /dev/null
test1.conf: # this is a line in the config file
Caught exception ios_base::clear: unspecified iostream_category error

When I don't pipe to /dev/null (but to stdout or a regular file), all is fine. I first suspected that the cout and cerr where causing problems, but I'm not sure.

I finally found that I could resolve this by enabling this line after opening the file, so that the badbit-type of exceptions are ignored.

file.exceptions(std::ios::failbit);

Frankly, I'm too novice in C++ to understand what is going on here.

My questions: what is causing the unspecified iostream_category exception? How can I avoid it? Is setting file.exceptions(std::ios::failbit); indeed a proper solution, or does that give other pitfalls? (A pointer to a good source detailing best practices for opening files in C++, which does included all proper exception handling, or some background explained, is highly appreciated!)

MacFreek
  • 3,207
  • 2
  • 31
  • 41
  • I'm still hoping for a good answer, but the closest I found was a related question [C++ ifstream failbit and badbit](http://stackoverflow.com/questions/11085151/c-ifstream-failbit-and-badbit), and a link to an article titled [Reading files line by line in C++ using ifstream: dealing correctly with badbit, failbit, eofbit, and perror()](https://gehrcke.de/2011/06/reading-files-in-c-using-ifstream-dealing-correctly-with-badbit-failbit-eofbit-and-perror/) by Jan-Philip Gehrcke. – MacFreek Dec 30 '16 at 14:07

2 Answers2

2

I would recommend the following approach. This is based on my own experience as well as on some of the links that have been provided above. In short I would suggest the following:

  1. Don't turn on the exceptions when using C++ streams. They are just so hard to get right I find they make my code less readable, which sort of defeats the purpose of exceptions. (IMHO it would be better if the C++ streams used exceptions by default and in a more reasonable manner. But since it isn't built that way it is better not to force the issue and just follow the pattern that the designers seem to have had in mind.)
  2. Rely on the fact that getline will handle the various stream bits properly. You don't need to check after each call if the bad or fail bits are set. The stream returned by getline will be implicitly cast to false when these occur.
  3. Restructure your code to follow the RAII pattern so that you don't need to call open() or close() manually. This not only simplifies your code, but ensures that you don't forget to close it. If you are not familiar with the RAII pattern, see https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization.
  4. Don't repeatedly put things like errno or your filename into the exceptions you generate. Keep them minimal to avoid repetition and use a catch block at the bottom to handle the errors, possibly throwing a new exception that adds the details you wish to report.

With that said, I would recommend rewriting your code to look something like the following:

using namespace std;
try {
    errno = 0;

    // Construct the stream here (part of RAII) then you don't need to call
    // open() or close() manually.
    ifstream file(config_file);   
    if (!file.is_open()) {
        throw invalid_argument("Could not open the file");
    }

    while (getline(file, line)) {
        // do all your processing as if no errors will occur (getline will 
        // be explicitly cast to a false when an error occurs). If there is
        // something wrong with a line (bad format, etc.) then throw an
        // exception without echoing it to cerr.
    }
    if (file.bad()) {
        throw invalid_argument("Problem while reading file");
    }
}
catch (const invalid_argument& e) {
    // Whatever your error handling needs to be. config_file should still
    // be valid so you can add it here, you don't need to add it to each
    // individual exception. Also you can echo to cerr here, you don't need
    // to do it inside the loop. If you want to use errno it should still
    // be set properly at this point. If you want to pass the exception to
    // the next level you can either rethrow it or generate a new one that
    // adds additional details (like strerror and the filename).
}
Steven W. Klassen
  • 1,401
  • 12
  • 26
1

I've improved on my earlier answer by writing a couple of functions that handle the stream checks using lambdas to actually process the file. This has the following advantages:

  1. You don't forget where to put the stream checks.
  2. Your code concentrates on what you want to do (the file processing) and not on the system boilerplate items.

I've created two versions. With the first your lambda is given the stream and you can process it how you like. With the second your lambda is given one line at a time. In both cases if an I/O problem occurs it will throw a system_error exception. You can also throw your own exceptions in your lambda and they will be passed on properly.

namespace {
    inline void throwProcessingError(const string& filename, const string& what_arg) {
        throw system_error(errno, system_category(), what_arg + " " + filename);
    }
}

void process_file(const string& filename, function<void (ifstream &)> fn) {
    errno = 0;
    ifstream strm(filename);
    if (!strm.is_open()) throwProcessingError(filename, "Failed to open");
    fn(strm);
    if (strm.bad()) throwProcessingError(filename, "Failed while processing");
}

void process_file_line_by_line(const string& filename, function<void (const string &)> fn)
{
    errno = 0;
    ifstream strm(filename);
    if (!strm.is_open()) throwProcessingError(filename, "Failed to open");
    string line;
    while (getline(strm, line)) {
        fn(line);
    }
    if (strm.bad()) throwProcessingError(filename, "Failed while processing");
}

To use them, you would call them as follows...

process_file("myfile.txt", [](ifstream& stream) {
    ... my processing on stream ...
});

or

process_file_line_by_line("myfile.txt", [](const string& line) {
    ... process the line ...
});
Steven W. Klassen
  • 1,401
  • 12
  • 26