reading external input file with no data

I am writing a program to read an external input file. And one of the conditions i'm trying to put in is if there is NO data in the external file, the program should return an error message, then exit.

But I don't know what to write. I can't use

if(!inputfile)

because that is only if there is no actual file. In my case, there is actually a file, but no data within it. Can anyone help?

Thanks!
What are you doing with the data when the file is not empty? What code have you written so far?

when the date is not empty, I am reading the data from the input file, making calculations, then writing it to an output file. I've got everything done except for this one part. Below, I've got the code for opening the input/output files...then the "wrong" code that i'm trying to change...


ifstream infile ("C:\\5\\5ExFileIn2.txt"); //create input file object
ofstream outfile ("C:\\5\\5ExFileOut2.txt"); //create output file object


if (!infile)
{
cerr<<InputFileError<<endl<<endl;
exit(1);
}

The easiest way to do this is to set the exception flag on the stream (infile.exceptions(std::ios::badbit)) and attempt to read the data. If you do not have all the data needed to run your calculations, an exception will be thrown (std::ios::failure) which can be caught, and an error returned.


ok...i'll give a shot...thanks for the advice!
NO! STOP listening to this over complicated vodoo! Simple solution test your input for the eof flag, done. This will be read where? That's right at the end of the file and if there is no data in the file then guess what the input will be. None of this badbit what does the rest of the code look like stuff. Keep it simple.

EDIT: Test the first input instance for eof, before your read loop otherwise your application will throw this error everytime it ends.
Last edited on
eof flag is probably the easiest way. You start with an ifstream. Open the file in question. If !ifstream, you failed to open the file. Exit. Else check for eof. If you get that you'd still identify your error. Otherwise you can start reading until you hit an end of file, which will invalidate the stream (!ifstream == true). Then you can spit out the data or whatever you plan to do with it. Badbit really would also work but I think that's a bit unnecessary for this.
If you have a fixed number of elements to read in, using exceptions can be useful.

Stupid example:

1
2
3
4
5
6
7
8
9
10
11
12
13
// Must read in exactly three elements.
bool read_input(istream& infile)
{
    int a, b, c;
    infile.exceptions(std::ios::failbit);
    try {
        infile >> a >> b >> c;
    } catch (std::ios::failure& ex) {
        std::cout << "Not enough input data." << std::endl;
        return false;
    }
    return true;
}


(And note that my earlier post was incorrect -- you need to use failbit, not badbit.)

If you were to test the status of infile for each of those reads, your code would be much longer and much less clear. Line 7 would be split across multiple lines and your have a test after each one. Or you could just test the status of the stream after line 7.

On the other hand, if you are reading a variable number of elements, then this is certainly not the best approach.

Exceptions are not complicated voodoo. This is 2010, not 1993.
Last edited on
Topic archived. No new replies allowed.