#include <cstdlib>
#include <cctype>
#include <vector>
#include <string>
#include <iterator>
#include <iostream>
#include <fstream>
struct person
{
static constexpr auto delimiter = ',';
std::string name;
std::string gender;
unsigned age;
};
std::ostream& operator<<(std::ostream &os, person const &p)
{
return os << p.name << " (" << p.gender << ", " << p.age << ')';
}
std::istream& operator>>(std::istream &is, person &p)
{
std::string name;
if (!std::getline(is, name, person::delimiter))
return is;
for (int ch{ is.peek() }; ch != EOF && std::isspace(ch); ch = is.peek())
is.get();
std::string gender;
if (!std::getline(is, gender, person::delimiter))
return is;
unsigned age;
if (!(is >> age))
return is;
p = person{ name, gender, age };
return is;
}
int main()
{
auto filename{ "foo.txt" };
std::ifstream is{ filename };
if (!is.is_open()) {
std::cerr << "Couldn't open \"" << filename << "\" for reading :(\n\n";
return EXIT_FAILURE;
}
std::vector<person> club{ std::istream_iterator<person>{ is }, std::istream_iterator<person>{} };
std::copy(std::begin(club), std::end(club), std::ostream_iterator<person>{ std::cout, "\n" });
}
foo.txt
Franz Xaver, male, 42
Schlumpfine, female, 12
Hugo Foobar, lol, 45