#include <iostream>
#include <vector>
#include <iomanip>
struct Person {
unsigned int id;
std::string name;
uint8_t age;
// ...
};
struct delim_field_extractor_proxy;
struct delim_field_extractor_proxy {
delim_field_extractor_proxy(std::string& field_ref, char delim = '"')
: field_ref_(field_ref), delim_(delim) {}
friend
std::istream& operator>>(std::istream& is, const delim_field_extractor_proxy& extractor_proxy);
void extract_value(std::istream& is) const {
field_ref_.clear();
char input;
bool addChars = false;
while(is) {
is.get(input);
if(is.eof()) {
break;
}
if(input == delim_) {
addChars = !addChars;
if(!addChars) {
break;
}
else {
continue;
}
}
if(addChars) {
field_ref_ += input;
}
}
// consume whitespaces
while(std::isspace(is.peek())) {
is.get();
}
}
std::string& field_ref_;
char delim_;
};
std::istream& operator>>(std::istream& is, const delim_field_extractor_proxy& extractor_proxy) {
extractor_proxy.extract_value(is);
return is;
}
int main() {
std::istream& ifs = std::cin; // Open file alternatively
std::vector<Person> persons;
Person actRecord;
int act_age;
while(ifs >> actRecord.id
>> delim_field_extractor_proxy(actRecord.name,'"')
>> act_age) {
actRecord.age = uint8_t(act_age);
persons.push_back(actRecord);
}
for(auto it = persons.begin();
it != persons.end();
++it) {
std::cout << it->id << ", " << it->name << ", " << int(it->age) << std::endl;
}
return 0;
}
ICAgICNpbmNsdWRlIDxpb3N0cmVhbT4KICAgICNpbmNsdWRlIDx2ZWN0b3I+CiAgICAjaW5jbHVkZSA8aW9tYW5pcD4KCiAgICBzdHJ1Y3QgUGVyc29uIHsKICAgICAgICB1bnNpZ25lZCBpbnQgaWQ7CiAgICAgICAgc3RkOjpzdHJpbmcgbmFtZTsKICAgICAgICB1aW50OF90IGFnZTsKICAgICAgICAvLyAuLi4KICAgIH07CgogICAgc3RydWN0IGRlbGltX2ZpZWxkX2V4dHJhY3Rvcl9wcm94eTsKCiAgICBzdHJ1Y3QgZGVsaW1fZmllbGRfZXh0cmFjdG9yX3Byb3h5IHsKICAgICAgICBkZWxpbV9maWVsZF9leHRyYWN0b3JfcHJveHkoc3RkOjpzdHJpbmcmIGZpZWxkX3JlZiwgY2hhciBkZWxpbSA9ICciJykgCiAgICAgICAgOiBmaWVsZF9yZWZfKGZpZWxkX3JlZiksIGRlbGltXyhkZWxpbSkge30KICAgICAgICAKICAgICAgICBmcmllbmQgCiAgICAgICAgc3RkOjppc3RyZWFtJiBvcGVyYXRvcj4+KHN0ZDo6aXN0cmVhbSYgaXMsIGNvbnN0IGRlbGltX2ZpZWxkX2V4dHJhY3Rvcl9wcm94eSYgZXh0cmFjdG9yX3Byb3h5KTsKICAgIAkKICAgIAl2b2lkIGV4dHJhY3RfdmFsdWUoc3RkOjppc3RyZWFtJiBpcykgY29uc3QgewogICAgCQlmaWVsZF9yZWZfLmNsZWFyKCk7CiAgICAJCWNoYXIgaW5wdXQ7CiAgICAgICAgICAgIGJvb2wgYWRkQ2hhcnMgPSBmYWxzZTsKICAgICAgICAgICAgd2hpbGUoaXMpIHsKICAgICAgICAgICAgICAgIGlzLmdldChpbnB1dCk7CiAgICAgICAgICAgICAgICBpZihpcy5lb2YoKSkgewogICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYoaW5wdXQgPT0gZGVsaW1fKSB7CiAgICAgICAgICAgICAgICAgICAgYWRkQ2hhcnMgPSAhYWRkQ2hhcnM7CiAgICAgICAgICAgICAgICAgICAgaWYoIWFkZENoYXJzKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAJY29udGludWU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYoYWRkQ2hhcnMpIHsKICAgIAkJCSAgICBmaWVsZF9yZWZfICs9IGlucHV0OwogICAgICAgICAgICAgICAgfQogICAgCQl9CiAgICAJCS8vIGNvbnN1bWUgd2hpdGVzcGFjZXMKICAgIA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1lIDw8ICIsICIgPDwgaW50KGl0LT5hZ2UpIDw8IHN0ZDo6ZW5kbDsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIDA7CiAgICB9
MTI2Nzg2NyAmcXVvdDtKb2huIFNtaXRoJnF1b3Q7ICAgICAgICAgMzIgICAKNjc1NDUgICAgICZxdW90O0phbmUgRG9lJnF1b3Q7ICAgICAgICAgICAzNiAgCjg2Nzc0NTMgJnF1b3Q7R3d5bmV0aCBNaWxsZXImcXVvdDsgICA1NiAgCjc1NTQzICAgICAmcXVvdDtKLiBSb3NzIFVudXN1YWwmcXVvdDsgMjMgIA==
1267867 "John Smith" 32
67545 "Jane Doe" 36
8677453 "Gwyneth Miller" 56
75543 "J. Ross Unusual" 23
1267867, John Smith, 32
67545, Jane Doe, 36
8677453, Gwyneth Miller, 56
75543, J. Ross Unusual, 23