Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <string>
- #include <fstream>
- #include <vector>
- using namespace std;
- class Chara {
- public:
- string name;
- int HP;
- friend ostream &operator<<(ostream &, Chara);
- friend istream &operator>>(istream &, Chara &);
- };
- ostream &operator<<(ostream &out, Chara c) {
- out << c.name << " " << c.HP;
- return out;
- }
- istream &operator>>(istream &in, Chara &c) {
- in >> c.name >> c.HP;
- return in;
- }
- int main() {
- vector<Chara> ch;
- ifstream fin("chara.txt");
- if (!fin) {
- cerr << "指定したファイルが見つかりません" << endl;
- exit(1);
- }
- while (fin) {
- Chara c;
- fin >> c;
- if (c.name != "") ch.push_back(c);
- }
- fin.close();
- for (Chara c: ch) cout << c << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement