代码中文件读取函数read_file()存在一些问题,望指出.
`#include
include
define SIZE 100
using namespace std;
static int nums_stu = 0;
typedef struct Student {
char* name;
char* ID; //占10个字符
double score_one;
double score_two;
double score_three;
double total_scores;
}Student;
Student Create_stu(); //创建学生信息结构体
void Input_student_score(Student* stu); //输入学生信息
void Output_student_score(Student stu); //输出所有学生信息
void select_student(char* ID, Student* stu, int size); //根据ID查找学生信息
void update_student_score(char* ID, int size, double score_one, double score_two, //更新学生信息
double score_three, Student* stu);
void delete_student_score(char* ID, int size, Student* stu); //删除学生信息
int read_file(Student* stu); //读取文件
void write_file(Student* stu); //写入文件
int main()
{
Student stu[SIZE];
Student stu2[SIZE];
for (int i = 0; i < 2; i++) {
/*stu[i] = Create_stu();*/
stu2[i] = Create_stu();
/*Input_student_score(&stu[i]);*/
}
/*write_file(stu);*/
int nums = read_file(stu2);
for (int i = 0; i < nums; i++)
{
Output_student_score(stu2[i]);
}
/*cout << nums_stu << endl;
while (true) {
cout << "----------------------------------------------------------------" << endl;
cout << "输入: 1.查找学生信息 2.更新学生信息 3.删除学生信息 4.输出所有学生信息 5.退出系统" << endl;
cout << "-" << endl;
int choice;
cin >> choice;
char ID[10];
switch (choice) {
case 1:
cout << "请输入要查找的学生ID:" << endl;
cin >> ID;
select_student(ID, stu, nums_stu);
break;
case 2:
cout << "请输入要更新的学生ID:" << endl;
cin >> ID;
double score_one, score_two, score_three;
cout << "请输入新的数学成绩:" << endl;
cin >> score_one;
cout << "请输入新的英语成绩:" << endl;
cin >> score_two;
cout << "请输入新的计算机成绩:" << endl;
cin >> score_three;
update_student_score(ID, nums_stu, score_one, score_two, score_three, stu);
break;
case 3:
cout << "请输入要删除的学生ID:" << endl;
cin >> ID;
delete_student_score(ID, nums_stu, stu);
break;
case 4:
for (int i = 0; i < nums_stu; i++) {
if (stu[i].ID != NULL) {
Output_student_score(stu[i]);
}
}
break;
case 5:
return 0;
default:
cout << "输入错误(或不符合规则),请重新输入!" << endl;
break;
}
}*/
return 0;
}
Student Create_stu() //创建学生信息结构体
{
Student* stu = new Student;
stu->name = new char[20];
stu->ID = new char[10];
stu->score_one = 0.0;
stu->score_two = 0.0;
stu->score_three = 0.0;
stu->total_scores = 0.0;
return *stu;
}
void Input_student_score(Student* stu) //输入学生信息
{
cout << "Please input the name of student :" << ":" << endl;
cin >> stu->name;
cout << "Please input the ID of student :" << endl;;
cin >> stu->ID;
cout << "Please input the math scores of student :" << endl;
cin >> stu->score_one;
cout << "Please input the english scores of student :" << endl;
cin >> stu->score_two;
cout << "Please input the computer scores of student :" << endl;
cin >> stu->score_three;
cout << endl << endl;
stu->total_scores = stu->score_one + stu->score_two + stu->score_three;
nums_stu++;
}
void Output_student_score(Student stu) //输出学生信息
{
cout << "Name:" << stu.name << endl;
cout << "ID:" << stu.ID << endl;
cout << "Math Scores:" << stu.score_one << endl;
cout << "English Scores:" << stu.score_two << endl;
cout << "Computer Scores:" << stu.score_three << endl;
cout << "Total Scores:" << stu.total_scores << endl;
}
void select_student(char* ID, Student* stu, int size) //根据ID查找学生信息
{
for (int i = 0; i < size; i++) {
if (!strcmp(stu[i].ID, ID)) {
Output_student_score(stu[i]);
return;
}
}
}
void update_student_score(char* ID, int size, double score_one, double score_two, //更新学生信息
double score_three, Student* stu)
{
for (int i = 0; i < size; i++) {
if (!strcmp(stu[i].ID, ID)) {
stu[i].score_one = score_one;
stu[i].score_two = score_two;
stu[i].score_three = score_three;
stu[i].total_scores = score_one + score_two + score_three;
return;
}
}
}
void delete_student_score(char* ID, int size, Student* stu) //删除学生信息
{
for (int i = 0; i < size; i++) {
if (!strcmp(stu[i].ID, ID)) {
stu[i].ID = NULL;
stu[i].score_one = 0;
stu[i].score_two = 0;
stu[i].score_three = 0;
stu[i].total_scores = 0;
nums_stu--;
delete[] stu[i].name;
delete[] stu[i].ID;
cout << "删除成功!" << endl;
return; //删除后,将该位置的ID置为NULL
}
}
}
int read_file(Student* stu)
{
FILE* fp;
int nums = 0;
fopen_s(&fp, "D:\OneDrive\桌面\123.txt", "r");
if (fp == NULL) {
cout << "文件打开失败!" << endl;
return -1;
}
while (fscanf_s(fp, "%18s %9s %lf %lf %lf %lf\n", stu->name, sizeof(stu->name), stu->ID, sizeof(stu->ID),
&stu->score_one, &stu->score_two,&stu->score_three, &stu->total_scores) != EOF)
{
nums++;
stu++;
}
cout << "读取成功!" << endl;
fclose(fp);
return nums;
}
void write_file(Student* stu)
{
FILE* fp;
fopen_s(&fp, "D:\OneDrive\桌面\123.txt", "w");
if (fp == NULL) {
cout << "文件打开失败!" << endl;
return;
}
for (int i = 0; i < nums_stu; i++) {
if (stu[i].ID != NULL) {
fprintf_s(fp, "%s %s %lf %lf %lf %lf\n", stu[i].name, stu[i].ID, stu[i].score_one, stu[i].score_two,
stu[i].score_three, stu[i].total_scores);
}
}
cout << "写入成功!" << endl;
fclose(fp);
}`