有五个学生,每个学生有三门课成绩,从键盘输入学生的学号,姓名,三门课成绩,把他们放到磁盘文件中,并从该文件能读出显示
这个问题大致需要以下几个步骤:
创建一个C++结构体来存储学生的信息和成绩。
从标准输入中读取五个学生的信息和成绩,并将它们存储到结构体中。
将五个学生的信息和成绩写入到磁盘文件中。
从磁盘文件中读取学生的信息和成绩,并显示到标准输出中。
下面是完整的C++代码示例:
#include <iostream>
#include <fstream>
using namespace std;
// 定义一个结构体来存储学生信息
struct Student {
int id;
string name;
double score[3];
};
int main() {
Student students[5];
// 从标准输入中读取五个学生的信息和成绩
for(int i = 0; i < 5; i++) {
cout << "Please enter student " << i + 1 << "'s id, name and three scores: ";
cin >> students[i].id >> students[i].name >> students[i].score[0] >> students[i].score[1] >> students[i].score[2];
}
// 将五个学生的信息和成绩写入到磁盘文件中
ofstream ofs("students.txt");
for(int i = 0; i < 5; i++) {
ofs << students[i].id << " " << students[i].name << " ";
for(int j = 0; j < 3; j++) {
ofs << students[i].score[j] << " ";
}
ofs << endl;
}
ofs.close();
// 从磁盘文件中读取学生的信息和成绩,显示到标准输出中
ifstream ifs("students.txt");
while(!ifs.eof()) {
Student student;
ifs >> student.id >> student.name;
for(int i = 0; i < 3; i++) {
ifs >> student.score[i];
}
if(!ifs.eof()) {
cout << student.id << " " << student.name << " ";
for(int i = 0; i < 3; i++) {
cout << student.score[i] << " ";
}
cout << endl;
}
}
ifs.close();
return 0;
}
上面的代码会从标准输入中读取五个学生的信息和成绩,并将它们存储到一个结构体数组中。然后,将这些信息和成绩写入到名为"students.txt"的磁盘文件中。最后,从该文件中读取学生的信息和成绩,并在标准输出上显示出来。需要注意的是,在读取磁盘文件时,需要判断文件是否已经结束,以避免读取到无效的数据。
可以按照以下步骤进行:
1、首先定义一个结构体 Student,用来存储学生信息,包括学号、姓名和三门课成绩。
2、定义文件名 filename,并创建输出文件流 outfile,将数据写入磁盘文件。
3、判断输出文件流是否打开成功,如果没有成功则输出错误提示,并返回 1。
4、使用循环语句输入五个学生的信息,并将学生信息写入磁盘文件。
5、关闭输出文件流 outfile。
6、创建输入文件流 infile,从磁盘文件读取数据,并逐行输出学生信息。
7、判断输入文件流是否打开成功,如果没有成功则输出错误提示,并返回 1。
8、输出表头,然后使用循环语句逐行读取文件内容,并输出学生信息。
9、关闭输入文件流 infile。
以下为详细代码:
#include <iostream>
#include <fstream>
using namespace std;
struct Student {
string id; // 学号
string name; // 姓名
double score1; // 第一门课成绩
double score2; // 第二门课成绩
double score3; // 第三门课成绩
};
int main() {
// 定义文件名
string filename = "student.txt";
// 创建文件流
ofstream outfile(filename, ios::out);
// 检查文件是否成功创建
if (!outfile) {
cout << "Error: Cannot open file(" << filename << ")." << endl;
return 1;
}
// 输入五个学生的信息并写入文件
for (int i = 1; i <= 5; i++) {
Student stu;
cout << "请输入第" << i << "个学生的信息:" << endl;
cout << "学号:";
cin >> stu.id;
cout << "姓名:";
cin >> stu.name;
cout << "第一门课成绩:";
cin >> stu.score1;
cout << "第二门课成绩:";
cin >> stu.score2;
cout << "第三门课成绩:";
cin >> stu.score3;
// 写入文件
outfile << stu.id << "\t" << stu.name << "\t" << stu.score1 << "\t" << stu.score2 << "\t" << stu.score3 << endl;
}
// 关闭文件流
outfile.close();
// 从文件中读取并输出信息
ifstream infile(filename, ios::in);
// 检查文件是否成功打开
if (!infile) {
cout << "Error: Cannot read file(" << filename << ")." << endl;
return 1;
}
// 输出表头
cout << "学号\t姓名\t第一门课成绩\t第二门课成绩\t第三门课成绩" << endl;
// 逐行读取数据
while (!infile.eof()) {
Student stu;
infile >> stu.id >> stu.name >> stu.score1 >> stu.score2 >> stu.score3;
// 输出学生信息
cout << stu.id << "\t" << stu.name << "\t" << stu.score1 << "\t\t" << stu.score2 << "\t\t" << stu.score3 << endl;
}
// 关闭文件流
infile.close();
return 0;
}