You could define a class to hold the data for each person:
class Person {
public:
std::string name;
std::string study_path;
std::string semester;
unsigned int age;
};
Then you can define a stream extraction operator for that class:
std::istream & operator>>(std::istream & stream, Person & person) {
stream >> person.name >> person.study_path >> person.semester >> person.age;
return stream;
}
And then you can just read the entire file like that:
std::ifstream file("datafile.txt");
std::vector<Person> data;
std::copy(std::istream_iterator<Person>(file), std::istream_iterator<Person>(),
std::back_inserter(data));
This will read the entire file and store all the extracted records in a vector
. If you know the number of records you will read in advance, you can call data.reserve(number_of_records)
before reading the file. Thus, the vector will have enough memory to store all records without reallocation, which might potentially speed up the loading if the file is large.