Problem in reading from a file.

I don't know much about programming to properly label the problem, but hear me out first.

I am to create a program that is to read input from a file.

The format of data from the file is as follows. 2 strings, followed by 10 integers.
<string1> <string2> <int1> <int2> <int3> <int4> <int5> <int6> <int7> <int8> <int9> <int10>

the format is the same per line, as many lines as necessary.

this part I get. Now comes error trapping.

what if somewhere along the file, one line is lacking 1 or more integers?

The code I wrote is set to read 2 strings and 10 integers per line(done using a loop), how will I skip the excess input operations if there are less than 10 integers?



-------------

A friend suggested I do a comparison if the input is of type int or of type string, but I don't know how to do that.
Last edited on
Try something like this
1
2
3
4
5
6
7
8
9
std::string line, in_str[2];
int in_int[10], number_of_integers = 0;

std::getline( file, line );
std::stringstream ss( line );

ss >> in_str[0] >> in_str[1];
while( ss >> in_int[ number_of_integers] )
    number_of_integers++;
Code not tested. The idea is to loop until >> fails. When that happens, a stream can be evaluated to false.
Topic archived. No new replies allowed.