C
chaoticcranium
Right now, I am reading a file line by line using getline() using this
code:
// Read file line by line, store the first 3 values on each line
in a vector and store the vector in a vector of vectors
while(getline(peakReader, line)) {
istringstream lineStr(line);
vector<int> peak(3);
if (!(lineStr >> peak[0] >> peak[1] >> peak[2])) {
cerr << "Error reading peak file; will continue
without using peaks." << endl;
usePeaks = false;
break;
}
peaks.push_back(peak);
}
peakReader is an ifstream, line is a string.
I was wondering if there is an easy way to prevent this code from
reading the last line of the file without affecting any of the other
lines. The only way I can see doing it is by opening another input
file stream, iterating line-by-line through the file to tally the
number of lines in the file, and then check for the last line by its
line number, but I feel this is a rather lame way of doing it and
there has to be something better that can be accomplished in this one
loop. Thanks in advance for your help.
code:
// Read file line by line, store the first 3 values on each line
in a vector and store the vector in a vector of vectors
while(getline(peakReader, line)) {
istringstream lineStr(line);
vector<int> peak(3);
if (!(lineStr >> peak[0] >> peak[1] >> peak[2])) {
cerr << "Error reading peak file; will continue
without using peaks." << endl;
usePeaks = false;
break;
}
peaks.push_back(peak);
}
peakReader is an ifstream, line is a string.
I was wondering if there is an easy way to prevent this code from
reading the last line of the file without affecting any of the other
lines. The only way I can see doing it is by opening another input
file stream, iterating line-by-line through the file to tally the
number of lines in the file, and then check for the last line by its
line number, but I feel this is a rather lame way of doing it and
there has to be something better that can be accomplished in this one
loop. Thanks in advance for your help.