0

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.

DeDee
  • 1,860
  • 17
  • 26
user3776749
  • 597
  • 1
  • 7
  • 19
  • getline into a string 'line', put the 'line' into a istringstream and process it –  Nov 12 '14 at 20:48
  • `file.substr()` should be `line.substr()`, and `file.find()` should be `line.find()`. But you already has the `pos`, so the second `find()` is redundant. To make the parsing easier, you should use an `istringstream` to parse each line. You can then use `getline()` with a delimiter to read delimited substrings from the stream, or use the stream's `>>` operator when the delimiter is whitspace. I edited my answer to show that. – Remy Lebeau Nov 12 '14 at 23:52

2 Answers2

0

Once you have the file you want opened, you can use getline()

string line;
ifstream file;
file.open('/path/to/file');
while(!file.eof()){
    // delimiter by default is '\n'
    getline(file, line);
    cout << line << endl;
}
JMc
  • 355
  • 1
  • 6
  • 2
    You should not use `while(!file.eof())` – Borgleader Nov 12 '14 at 20:49
  • This is just a simple example. If I may ask, why not use eof()? (Besides the chance that a bunch of empty lines are at the end of the file). – JMc Nov 12 '14 at 20:52
  • Still wrong: Testing for EOF is wrong (in some cases it is reasonable) and omitting testing the extraction (getline) –  Nov 12 '14 at 20:52
  • What if I need to work with the individual numbers/integers on each line? – user3776749 Nov 12 '14 at 20:56
  • If you want to get the individual numbers, you will have to parse each line you get. Here is a link to parse a string in C++: http://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c"> – JMc Nov 12 '14 at 21:05
  • So what should I use as he terminating condition since !file.oef() is a bad call? – user3776749 Nov 12 '14 at 22:02
  • 1
    @user3776749 Put the actual input operator inside the parameters. Functions like `std::getline()` and `operator>>()` will return the stream and it will convert to a boolean, returning true or false based on successful input. – 0x499602D2 Nov 12 '14 at 22:10
  • stoi() converts strings to int – JMc Nov 14 '14 at 02:40
0

Try something like this:

std::ifstream inFile("/path/to/file");

std::string line;
int lineNum = 0;

while (std::getline(inFile, line))
{
    ++lineNum;

    std::istreamstream iss(line);
    int value;

    while (iss >> value)
    {
        // work with value depending on lineNum
    }
}
Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620