这是我的代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student
{
char studentName[50];
int id;
};
struct student_detail
{
int day, month, year, grade;
struct student information;
}stu_data;
//---------------------------------//
int main()
{
struct student_detail stu_data[10];
int student_no, i=0, choice=0;
char keyword[50];
FILE *fptr;
printf("Add-Information(1) || Get-Information(2): ");
scanf("%d",&choice);
if(choice == 1){
fptr = (fopen("userInfo.txt","ab"));
system("CLS");
printf("How many students would you like to add?[MAX 10]: ");
scanf("%d",&student_no);
system("CLS");
for(i=0; i < student_no; i++){
system("CLS");
printf("Enter student#%d's name: ",i+1);
scanf("%s", stu_data[i].information.studentName);
printf("\nWhat is %s's studentID?: ",stu_data[i].information.studentName);
scanf("%d",&stu_data[i].information.id);
printf("\nWhat is %s's date of birth?(dd/mm/yy):\n",stu_data[i].information.studentName);
scanf("%d %d %d",&stu_data[i].day, &stu_data[i].month, &stu_data[i].year);
fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].information.id, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].day, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].month, sizeof(struct student), 1, fptr);
fwrite(&stu_data[i].year, sizeof(struct student), 1, fptr);
}
fclose(fptr);
}
if(choice == 2){
fptr = (fopen("userInfo.txt","rb+"));
system("CLS");
printf("What students information would you like to retreive?: ");
scanf("%s",keyword);
fseek(fptr, sizeof(struct student), SEEK_SET);
fread(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
fread(&stu_data[i].information.id, sizeof(struct student), 1, fptr);
fread(&stu_data[i].day, sizeof(struct student), 1, fptr);
fread(&stu_data[i].month, sizeof(struct student), 1, fptr);
fread(&stu_data[i].year, sizeof(struct student), 1, fptr);
printf("Name: %s",stu_data[i].information.studentName);
printf("\nID: %d",stu_data[i].information.id);
printf("\nDate of birth: %d/%d/%d\n\n",stu_data[i].day, stu_data[i].month, stu_data[i].year);
system("PAUSE");
fclose(fptr);
return 0;
}
}
文件中的输入如下所示:
Name: Riley
ID: 1
Date of birth: 01/10/2001
当我从文件中读取信息时,我得到了正确的信息,但不是全部,在消息中读取时如下所示:
Name: y
ID: 1
Date of birth: 10/2001/2686248
写作只是不阅读(抱歉程序内缺少注释)。
最佳答案
我怀疑你正在写或读你想要的内容。
当你写作时,
fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
您正在编写 sizeof(struct Student)
字节的 1 个元素,不是从 Student 结构体的开头开始,而是从 stu_data[i].information.studentName
。这样你就得到了 50 字节的名称,以及内存中它后面发生的任何事情。如果 sizeof(struct Student)
是 250 字节,那么您(最多)会编写 200 字节的废话,然后继续下一个字段。啊!
除非您有充分的理由不这样做,否则为什么不立即写下整个记录呢?代码更少,对计算机的干扰更少。
fwrite(&stu_data[i], sizeof(struct student), 1, fptr);
通常,这就是您应该在代码中查找的内容:作为第一个参数提供的地址应该是其大小在第二个参数中指定的数据结构。更好的是:
fwrite(&stu_data[i], sizeof(stu_data[i]), 1, fptr);
同样的建议也适用于阅读。我只会指出另一个错误:
fseek(fptr, sizeof(struct student), SEEK_SET);
无论提供什么输入,您都会寻求(您可能想要的)第二条记录:从文件开头开始的 sizeof(struct Student)
字节。根据输入,您可能需要该数字的某个倍数。
此外,hexdump(1) 是你的 friend ,尤其是 -C 选项。
关于c - C语言程序从文件中读取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53416607/