What is the easiest (simplest) way to determine when you reach the end of a line of data while reading in from a plain text file using ifstream? Basically I need to do different things with the data based on its location (measured by line number) in the text file.
So if I had a text file with the following contents:
12 54 873 9 87 23
34 25 93 10 94 5 8
the first line I might want to store, but the second line I need to modify before storing, or discard, or do some other operation.
The straight file-read looking something like this
while (inFile >> temp)
//store temp;
EDIT:
I haven't tested this yet, bit it's what I've come up with. I haven't done a lot with the more complex string class functions so my syntax may be incorrect, but does it look like I'm on the right track?
// include the necessary libraries
string line, delim = ' ';
ifstream inFile;
int temp, lineNum = 1;
size_t pos = 0;
file.open('/path/to/file');
while(getline(file, line)){
while ((pos = line.find(delim)) != npos) {
temp = file.substr(0, file.find(delim);
line.erase(0, pos + delim.length());
// depending on the value of lineNum
// work with temp
}
lineNum++;
}
A quick question is if it can convert to int
(what I need it to be) when I assign it directly to temp or if I'll need to cast it as an int before working with it.