// person.h
#ifndef PERSON_H
#define PERSON_H
#include <string>
#include <vector>
struct Person {
std::string name;
int age;
std::string gender;
};
std::vector<Person> readFromFile(const std::string& filename);
void displayData(const Person& person);
#endif // PERSON_H
// person.cpp
#include "person.h"
#include <iostream>
#include <fstream>
std::vector<Person> readFromFile(const std::string& filename) {
std::vector<Person> people;
std::ifstream infile(filename);
if (!infile) {
std::cerr << "ファイルを開けませんでした" << std::endl;
return people;
}
Person person;
while (infile >> person.name >> person.age >> person.gender) {
people.push_back(person);
}
infile.close();
return people;
}
void displayData(const Person& person) {
std::cout << "名前: " << person.name << ", 年齢: " << person.age << ", 性別: " << person.gender << std::endl;
}
// main.cpp
#include <iostream>
#include <vector>
#include "person.h"
int main() {
std::vector<Person> people = readFromFile("data.txt");
// データを表示して確認します
for (const auto& person : people) {
displayData(person);
}
return 0;
}