user5556782
user5556782

Reputation:

How should I go about reading in a text file with each line containing various data types in C++?

I have a textfile with lots of data. Each row has two integers which specify coordinates, followed by the name of the coordinate, and additional attributes.

I'm trying to read in all the attributes into a vector and then later access the different attributes and each row.

I'm using getline to read in each line, but how would I be able to access the various attributes instead of the whole line?

Here is my code:

    while (getline(location_file, line)) {
          vector<string> file;
          file.push_back(line);
}

Would structs be a better option?

Upvotes: 0

Views: 44

Answers (1)

Paul92
Paul92

Reputation: 9062

Instead of using getline to read the whole line, since the input seems to be standard, you can use a std::istream and the >> operator to read formatted input that ignores whitespaces. You basically have to read 2 integers and then use getline to read the rest of the line.

Upvotes: 1

Related Questions