Предположим, что мы имеем следующую ситуацию:
- Структура записи объявляется следующим образом
struct Person {
unsigned int id;
std::string name;
uint8_t age;
// ...
};
- Записи хранятся в файле в следующем формате:
ID Forename Lastname Age
------------------------------
1267867 John Smith 32
67545 Jane Doe 36
8677453 Gwyneth Miller 56
75543 J. Ross Unusual 23
...
Файл должен быть прочитан для сбора произвольного количества записей Person
, упомянутых выше:
std::istream& ifs = std::ifstream("SampleInput.txt");
std::vector<Person> persons;
Person actRecord;
while(ifs >> actRecord.id >> actRecord.name >> actRecord.age) {
persons.push_back(actRecord);
}
if(!ifs) {
std::err << "Input format error!" << std::endl;
}
Вопрос: (часто задаваемый вопрос в той или иной форме)
Что я могу сделать, чтобы читать отдельные значения, сохраняя их значения в поля actRecord
variables?
Приведенный выше пример кода заканчивается ошибками времени выполнения:
Runtime error time: 0 memory: 3476 signal:-1
stderr: Input format error!