首先,我会告诉你我是如何定义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);
}
}
如果您还需要其他任何内容,请告知我,并确保添加它,谢谢。
答案 0 :(得分:0)
此代码存在一些问题:
for(i=0; i < studentRecordSize; i ++){
//adds the new student at the end of the array.
if(i == studentRecordSize){ // <- This can never be TRUE !!!
strncpy(*studentRecordList[i].fname, &fname,20); // Wrong
strncpy(*studentRecordList[i].lname, &lname,20);
studentRecordList[i].GPA = GPA;
strncpy(*studentRecordList[i].SN, &SN,10);
}
// Should the code below be an else ?
// If not you'll access outside tempList
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);
}
也许你想这样做:
for(i=0; i < studentRecordSize; i ++){
if(i == (studentRecordSize-1)){
// Add the new record
strncpy(studentRecordList[i].fname, fname,20);
strncpy(studentRecordList[i].lname, lname,20);
studentRecordList[i].GPA = GPA;
strncpy(studentRecordList[i].SN, SN,10);
}
else
{
// Copy from the temp list
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);
}
}
添加功能的更简单版本可以是:
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;
for(i = 0; i < studentRecordSize; i ++){
tempList = studentRecordList[i];
}
free(studentRecordList);
studentRecordSize = studentRecordSize+1;
studentRecordList = (StudentRecord*)malloc(studentRecordSize * sizeof(StudentRecord));
for(i=0; i < (studentRecordSize-1); i ++){
studentRecordList[i] = tempList[i];
}
free(tempList);
// Add the new record
strncpy(studentRecordList[(studentRecordSize-1)].fname, fname,20);
strncpy(studentRecordList[(studentRecordSize-1)].lname, lname,20);
studentRecordList[(studentRecordSize-1)].GPA = GPA;
strncpy(studentRecordList[(studentRecordSize-1)].SN, SN,10);
}
答案 1 :(得分:0)
请试试这个: 在save_records()中打开文件二进制文件
fopen(student_file , "wb");
然后使用fwrite写入记录
fwrite( &studentRecordList[i] , sizeof( studentRecordList) , 1 , student_file );
如果可以,还可以将记录数量写入打开文本的单独文件中。