【问题标题】:Reading information from file program in C从C中的文件程序中读取信息
【发布时间】:2018-11-21 16:32:45
【问题描述】:

这是我的代码:

#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;
    }
}  

文件的输入如下所示:

姓名:莱利
编号:1
出生日期:01/10/2001

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

姓名:y
编号:1
出生日期:10/2001/2686248

写作工作只是不阅读(抱歉程序中缺少 cmets)。

【问题讨论】:

  • 请提供每次使用scanf的返回值。不是扫描的值,是返回的值,表示扫描成功的次数。
  • 为什么每个fread 的大小都是sizeof(struct student)
  • fwrite(&amp;stu_data[i], sizeof(stu_data[i]), 1, fptr); 是将一个条目写入文件的正确方法。阅读也一样。

标签: c arrays file


【解决方案1】:

我怀疑你正在写或读你想要的。

当你写作时,

fwrite(&stu_data[i].information.studentName, sizeof(struct student), 1, fptr);

您正在编写 sizeof(struct student) 字节的 1 个元素,不是从学生结构的开头开始,而是从 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) 是你的朋友,尤其是 -C 选项。

【讨论】:

  • 谢谢!我现在才看到这个。阅读/写作技巧拯救了我的一天。
猜你喜欢
  • 2011-05-09
  • 1970-01-01
  • 2013-12-22
  • 1970-01-01
  • 2016-07-23
  • 2018-08-06
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
相关资源
最近更新 更多