用C语言从文件程序中读取信息

用C语言从文件程序中读取信息,c,arrays,file,C,Arrays,File,这是我的代码: #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; //------

这是我的代码:

#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;
    }
}  
#包括
#包括
#包括
结构学生
{
char studentName[50];
int-id;
};
结构学生详细信息
{
国际日、月、年、年级;
结构化学生信息;
}stu_数据;
//---------------------------------//
int main()
{
结构学生详细学生数据[10];
int学生号,i=0,choice=0;
字符关键字[50];
文件*fptr;
printf(“添加信息(1)|获取信息(2):”;
scanf(“%d”,选择(&C);
如果(选项==1){
fptr=(fopen(“userInfo.txt”,“ab”);
系统(“CLS”);
printf(“您希望添加多少名学生?[最多10名]:”;
scanf(“%d”和学生编号);
系统(“CLS”);
对于(i=0;i
文件中的输入如下所示:

姓名:赖利
ID:1
出生日期:2001年10月1日

当我从文件中读取信息时,我得到了正确的信息,但不是全部信息,当在消息中读取时,信息如下所示:

名称:y
ID:1
出生日期:10/2001/2686248


写作只是不阅读而已(很抱歉程序中没有评论)。

我怀疑你是在写作还是在阅读你想要的东西

当你写作时

fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);
您正在编写一个
sizeof(struct student)
字节的元素,不是从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)
bytes。根据输入,您可能需要该数字的倍数


另外,hextump(1)是您的朋友,尤其是-C选项

请提供每次使用scanf的返回值。不是扫描的值,而是返回的值,指示扫描成功的次数。为什么每个
fread
size
sizeof(struct student)
fwrite(&stu_数据[i],sizeof(stu数据[i]),1,fptr
将是向文件中写入一个条目的正确方式。雷德也一样,谢谢!我现在才有时间看这个。阅读/写作技巧挽救了我的一天。