#include <iostream>
#include <fstream>
#include <string>
struct Applicant {
std::string lastName;
int course;
std::string group;
double averageScore;
};
void writeToFile(const std::string& filename) {
std::ofstream file(filename);
if (!file) {
std::cerr << "Error opening file for writing";
return;
}
int numApplicants;
std::cout << "Enter the number of applicants: ";
std::cin >> numApplicants;
for (int i = 0; i < numApplicants; ++i) {
Applicant app;
std::cout << "Enter last name, course, group and average score for applicant " << i+1 << ": ";
std::cin >> app.lastName >> app.course >> app.group >> app.averageScore;
file << app.lastName << " " << app.course << " " << app.group << " " << app.averageScore << std::endl;
}
file.close();
}
void readFromFile(const std::string& filename) {
std::ifstream file(filename);
if (!file) {
std::cerr << "Error opening file for reading";
return;
}
std::cout << "List of applicants:" << std::endl;
Applicant app;
while(file >> app.lastName >> app.course >> app.group >> app.averageScore) {
std::cout << app.lastName << " " << app.course << " " << app.group << " " << app.averageScore << std::endl;
}
file.close();
}
int main() {
const std::string filename = "applicants.txt";
writeToFile(filename);
readFromFile(filename);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnN0cmVhbT4KI2luY2x1ZGUgPHN0cmluZz4KCnN0cnVjdCBBcHBsaWNhbnQgewogICAgc3RkOjpzdHJpbmcgbGFzdE5hbWU7CiAgICBpbnQgY291cnNlOwogICAgc3RkOjpzdHJpbmcgZ3JvdXA7CiAgICBkb3VibGUgYXZlcmFnZVNjb3JlOwp9OwoKdm9pZCB3cml0ZVRvRmlsZShjb25zdCBzdGQ6OnN0cmluZyYgZmlsZW5hbWUpIHsKICAgIHN0ZDo6b2ZzdHJlYW0gZmlsZShmaWxlbmFtZSk7CgogICAgaWYgKCFmaWxlKSB7CiAgICAgICAgc3RkOjpjZXJyIDw8ICJFcnJvciBvcGVuaW5nIGZpbGUgZm9yIHdyaXRpbmciOwogICAgICAgIHJldHVybjsKICAgIH0KCiAgICBpbnQgbnVtQXBwbGljYW50czsKICAgIHN0ZDo6Y291dCA8PCAiRW50ZXIgdGhlIG51bWJlciBvZ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