首先,我将向您展示如何定义 struct StudentRecord
typedef struct{
char SN[10];
char lname[20];
float GPA;
}StudentRecord;
现在的重点是从 .dat 文件中读取该程序成功执行的信息,但是当我去保存我制作的数组时(更多内容见下文),它给了我一个非常奇怪的输出此代码有2个全局变量
StudentRecord* studentRecordList;
int studentRecordSize;
studentRecordSize 将是文件中的第一个输入行,读取文件的函数如下所示。
void load_records(){
FILE* student_file = fopen("StudentRecordFile.dat", "r");
if(student_file == 0){
perror("cannot open StudentRecordFile.dat in load_records");
}
//free existing student list
if(studentRecordList != 0){
free(studentRecordList);
}
//get number of students in the list
fscanf(student_file, "%d",&studentRecordSize);
//create and load new student list
studentRecordList = (StudentRecord*)malloc(studentRecordSize * sizeof(StudentRecord));
int i;
for(i = 0; i < studentRecordSize;i++){
fscanf(student_file,"%s %s %s %f", studentRecordList[i].SN,studentRecordList[i].fname,studentRecordList[i].lname,&studentRecordList[i].GPA);
}
fclose(student_file);
}
现在,驱动程序为您提供了 4 个选项:0-退出(也保存到文件)、1-查找(仅在数组中搜索)、2-添加(将另一个学生添加到数组中)、3-修改,4-删除。
将学生添加到数组的函数是我遇到问题的地方,由于某种原因,它弄乱了我的输出文件,并将所有 GPA 变量更改为 0.000000,并在整个文档中留下随机空格。无论如何,这就是它的样子。
void add_record(){
char fname[20];
char lname[20];
char SN[10];
float GPA;
printf("Enter the Students first Name: ");
scanf("%s", &fname);
printf("\nEnter the Students last Name: ");
scanf("%s", &lname);
printf("\nEnter the Students number: ");
scanf("%s", &SN);
printf("\nEnter the Students GPA: ");
scanf("%f", &GPA);
StudentRecord* tempList;
tempList = (StudentRecord*)malloc(studentRecordSize * sizeof(StudentRecord));
int i;
studentRecordSize = studentRecordSize+1;
for(i = 0; i < studentRecordSize -1; i ++){
tempList[i].GPA = studentRecordList[i].GPA;
strncpy(tempList[i].SN, studentRecordList[i].SN,10);
strncpy(tempList[i].fname, studentRecordList[i].fname,20);
strncpy(tempList[i].lname , studentRecordList[i].lname,20);
}
printf("%f", &tempList[i].GPA );
free(studentRecordList);
studentRecordList = (StudentRecord*)malloc(studentRecordSize * sizeof(StudentRecord));
for(i=0; i < studentRecordSize; i ++){
//adds the new student at the end of the array.
if(i == studentRecordSize){
strncpy(*studentRecordList[i].fname, &fname,20);
strncpy(*studentRecordList[i].lname, &lname,20);
studentRecordList[i].GPA = GPA;
strncpy(*studentRecordList[i].SN, &SN,10);
}
strncpy(studentRecordList[i].fname, tempList[i].fname,20);
strncpy(studentRecordList[i].lname , tempList[i].lname,20);
studentRecordList[i].GPA = tempList[i].GPA;
strncpy(studentRecordList[i].SN , tempList[i].SN,10);
}
free(tempList);
}
此外,如果您好奇我的 save_records 函数是什么样子,否则请忽略。
void save_records(){
FILE* student_file = fopen("StudentRecordFile.dat", "w");
if(student_file == 0){
perror("cannot open StudentRecordFile.dat in load_records");
}
fprintf(student_file,"%d\n",studentRecordSize);
int i;
for(i = 0; i < studentRecordSize; i++){
fprintf(student_file,"%s\n%s\n%s\n%f\n", studentRecordList[i].SN, studentRecordList[i].fname, studentRecordList[i].lname, &studentRecordList[i].GPA);
}
}
如果您还需要任何其他信息,请告诉我,我一定会添加,谢谢。
最佳答案
请尝试这个: 在 save_records() 中打开您的文件二进制文件
fopen(student_file , "wb");
然后使用fwrite写入记录
fwrite( &studentRecordList[i] , sizeof( studentRecordList) , 1 , student_file );
如果可以的话,将记录数写入打开文本的单独文件中也更容易。
关于将数组的变量复制到另一个数组,该数组是带有字符的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33223309/