#include <iostream>
#include <fstream>
#include <string>
#include <vector>
class Person{
public:
std::string name;
int age;
Person(std::string name, int age):name(name), age(age){}
Person(){}
};
std::ostream& operator << (std::ostream &os, const Person &p){
return os << p.name << " " << p.age;
}
std::istream& operator >> (std::istream &in, Person &p){
in >> p.name >> p.age;
if(!in){
p = Person();
}
return in;
}
int main(){
std::vector<Person> people =
{
Person("Artem", 20),
Person("Dmitriy", 18),
Person("Bob", 23),
Person("Alice", 46)
};
std::ofstream out("/home/artem/Рабочий стол/EntryFile/NewFile");
if(out.is_open())
for(std::vector<Person>::iterator iter = people.begin();iter != people.end();iter++)
out << (*iter) << std::endl;
out.close();
std::vector<Person> users;
std::ifstream in("/home/artem/Рабочий стол/EntryFilee/NewFile");
if(in.is_open()){
Person user;
while(in >> user)
users.push_back(user);
}
std::cout << "All users: " << std::endl;
for(std::vector<Person>::iterator iter = users.begin(); iter != users.end();iter++){
std::cout << (*iter).name << " " << (*iter).age << std::endl;
}
}
Вывод:
All users:
Записалось нормально, но вывести не получается.