#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <cstdint>
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(int argc, char** argv) {
std::ifstream inputFile;
bool useInputFile = false;
if(argc > 1) {
inputFile.open(argv[1]);
useInputFile = true;
}
std::istream& ifs = useInputFile ? inputFile :
std::cin; // Open file alternatively
std::vector<Person> persons;
Person actRecord;
int act_age;
while(ifs >> actRecord.id
>> delim_field_extractor_proxy(actRecord.name,'\t')
>> 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;
}
ICAgICNpbmNsdWRlIDxpb3N0cmVhbT4KICAgICNpbmNsdWRlIDxmc3RyZWFtPgogICAgI2luY2x1ZGUgPHZlY3Rvcj4KICAgICNpbmNsdWRlIDxpb21hbmlwPgogICAgI2luY2x1ZGUgPGNzdGRpbnQ+CgogICAgc3RydWN0IFBlcnNvbiB7CiAgICAgICAgdW5zaWduZWQgaW50IGlkOwogICAgICAgIHN0ZDo6c3RyaW5nIG5hbWU7CiAgICAgICAgdWludDhfdCBhZ2U7CiAgICAgICAgLy8gLi4uCiAgICB9OwoKICAgIHN0cnVjdCBkZWxpbV9maWVsZF9leHRyYWN0b3JfcHJveHk7CgogICAgc3RydWN0IGRlbGltX2ZpZWxkX2V4dHJhY3Rvcl9wcm94eSB7CiAgICAgICAgZGVsaW1fZmllbGRfZXh0cmFjdG9yX3Byb3h5KHN0ZDo6c3RyaW5nJiBmaWVsZF9yZWYsIGNoYXIgZGVsaW0gPSAnIicpCiAgICAgICAgOiBmaWVsZF9yZWZfKGZpZWxkX3JlZiksIGRlbGltXyhkZWxpbSkge30KCiAgICAgICAgZnJpZW5kCiAgICAgICAgc3RkOjppc3RyZWFtJiBvcGVyYXRvcj4+KHN0ZDo6aXN0cmVhbSYgaXMsIGNvbnN0IGRlbGltX2ZpZWxkX2V4dHJhY3Rvcl9wcm94eSYgZXh0cmFjdG9yX3Byb3h5KTsKCiAgICAJdm9pZCBleHRyYWN0X3ZhbHVlKHN0ZDo6aXN0cmVhbSYgaXMpIGNvbnN0IHsKICAgIAkJZmllbGRfcmVmXy5jbGVhcigpOwogICAgCQljaGFyIGlucHV0OwogICAgICAgICAgICBib29sIGFkZENoYXJzID0gZmFsc2U7CiAgICAgICAgICAgIHdoaWxlKGlzKSB7CiAgICAgICAgICAgICAgICBpcy5nZXQoaW5wdXQpOwogICAgICAgICAgICAgICAgaWYoaXMuZW9mKCkpIHsKICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmKGlucHV0ID09IGRlbGltXykgewogICAgICAgICAgICAgICAgICAgIGFkZENoYXJzID0gIWFkZENoYXJzOwogICAgICAgICAgICAgICAgICAgIGlmKCFhZGRDaGFycykgewogICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgCWNvbnRpbnVlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGl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JkLmlkCiAgICAgICAgICAgICAgICAgID4+IGRlbGltX2ZpZWxkX2V4dHJhY3Rvcl9wcm94eShhY3RSZWNvcmQubmFtZSwnXHQnKQogICAgICAgICAgICAgICAgICA+PiBhY3RfYWdlKSB7CiAgICAgICAgCWFjdFJlY29yZC5hZ2UgPSB1aW50OF90KGFjdF9hZ2UpOwogICAgICAgICAgICBwZXJzb25zLnB1c2hfYmFjayhhY3RSZWNvcmQpOwogICAgICAgIH0KCiAgICAgICAgZm9yKGF1dG8gaXQgPSBwZXJzb25zLmJlZ2luKCk7CiAgICAgICAgICAgIGl0ICE9IHBlcnNvbnMuZW5kKCk7CiAgICAgICAgICAgICsraXQpIHsKICAgICAgICAJc3RkOjpjb3V0IDw8IGl0LT5pZCA8PCAiLCAiIDw8IGl0LT5uYW1lIDw8ICIsICIgPDwgaW50KGl0LT5hZ2UpIDw8IHN0ZDo6ZW5kbDsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIDA7CiAgICB9Cg==
1267867, John Smith, 32
67545, Jane Doe, 36
8677453, Gwyneth Miller, 56
75543, J. Ross Unusua, 23