【问题标题】:How Can I Read and Write User Information to file with structure如何读取和写入用户信息到具有结构的文件
【发布时间】:2019-05-01 16:25:52
【问题描述】:

我创建文件,我想从用户那里获取信息并写入文件或从数据中读取信息?我想在 Switch 中使用

我用来将数据写入文件的代码:fwrite(&x, sizeof(struct userRec), 1, fout); fclose(fout);

我用来将数据写入文件的代码:fread(&x, sizeof(struct userRec), 1, fin);

当我尝试在文件中写入一些信息并使用记事本打开时,我只看到一些 NULL 字符。

struct userRec{

    char firstName[25];
    char lastName[25];
    int UserID; 
    int day;
    int month;
    int year;
    char adress[200];
    float money;

};

int main(){

    struct userRec user;
    struct userRec x;
    int menu1, answer;
    char login[10];
    char answer2;
    char file_name[100];
    float deposit, withdraw;

    printf("Welcome to the IAU BANK !\n");

    printf("Please enter any character to sign in : ");
    scanf("%c",&login);

    printf("\n\n\tMENU\n1.Create User Registration\n2.Login to Account\n");
    scanf("%d",&menu1); 

    switch (menu1){

        case 1:

            printf("Please write your ID Number : ");
            scanf("%s",&file_name);
            FILE *fout;
            fout = fopen(file_name , "wb");

            printf("Please Enter Your First Name : ");
            scanf("%s",&x.firstName);

            printf("Please Enter Your Last Name : ");
            scanf("%s",&x.lastName);    

            printf("Please Enter Your Identification Number Again : ");
            scanf("%d",&x.UserID);

            printf("Please Enter Your Birthday Date (dd/mm/yyyy) Format : ");
            scanf("%d / %d / %d",&x.day,&x.month,&x.year);

            printf("Please Enter Your Adress : ");
            scanf("%s",&x.adress);

            printf("\n\n\tYour Information is :\nFirst Name : %s\nLast Name : %s \nIdentification Number : %d \nBirthday Date : %d/%d/%d\nAdress : %s",x.firstName,x.lastName,x.UserID,x.day,x.month,x.year,x.adress);

            printf("\n\n\tDo You Approve Your Information ? Yes:5 No:6 Enter 5 or 6\n");
            scanf("%d",&answer);

        case 5:

            printf("Your information has been saved!\n");

            fwrite(&user, sizeof(struct userRec), 1, fout); 
            fclose(fout);           

            printf("\nPress the 2 button to log in to your account : ");
            scanf("%d",&answer);

        case 2:

            printf("Please write your ID Number : ");
            scanf("%s",&file_name);

            FILE *fin;
            fin = fopen(file_name , "r");

            fread(&x, sizeof(struct userRec), 1, fin);

            printf("Welcome %s %s your ID : %d Your Date : %d / %d / %d Your Address : %s", x.firstName,x.lastName,x.UserID,x.day,x.month,x.year,x.adress);


            printf("\nThe amount of money in your account : %.2f",x.money);

            printf("\n\n\tMENU\n3.Deposit Money\n4.Withdraw Money\n");
            scanf("%d",&menu1);
            break;

        case 3:
            printf("Please enter the amount of money you want to deposit : ");
            scanf("%f",&deposit);

            printf("\nThe amount of money you deposit : %.2f\n", deposit);

            x.money = deposit + x.money;

            fwrite(&x, sizeof(struct userRec), 1, fout); 
            fclose(fout);
            printf("\nThe total amount of money you deposit : %.2f\n",x.money);
            break;

        case 4:

            printf("Please enter the amount of money you want to withdraw : ");
            scanf("%f",&withdraw);

    }
}

【问题讨论】:

  • 你的流程一团糟。您在几个case 中打开一个文件但不关闭它,然后在另一个case 中关闭它。您如何跟踪应该打开和关闭哪个?你有两个不同的userRec 结构,xuser,哪一个在哪个时间点有效?如果你填写user然后保存x呢?或者读入user 并显示xswitch 中也有很多失败点,因此也很难跟进。
  • 不要将case 5: 放在case 1:case 2: 之间。不要忘记在每个 case 块的末尾包含 break;。使用函数。
  • @Someprogrammerdude — 因为变量声明不是紧跟在 case 标签之后,所以允许它们不加注释。变量的范围很奇怪,但代码实际上并没有编译器可报告的问题。

标签: c file structure


【解决方案1】:

你当然可以这样做,但你真的想这样做吗?

主要缺点与文件的可移植性有关:字节序、对齐方式、处理器大小和版本

endian问题是存储超过128或256的数字类型需要使用多个字节,根据第一个字节存储数字的最高部分(大端)还是最低部分有两种主要模式(小端)。如果文件以一种形式存储在一台计算机上,则无法在以另一种方式配置的计算机上正确读取。世界上很多地方都在 Intel 或 little-endian 模式的 ARM 上运行,所以看起来 little-endian 赢了,但情况并非总是如此。

对齐是编译器对齐结构的每个字段以提供更好的性能的地方,留下奇数的备用字节。不同的处理器有不同的对齐规则,所以文件可能会在不同的处理器上被误读,甚至是在同一个平台上,你的程序构建的选项略有不同。

处理器大小是一个整数的默认大小多年来一直在增长的问题。如果您在 1980 年保存文件,您的 int 可能只占用 2 个字节,因为那是当时处理器的 int 大小。它逐步增长到 4 字节、8 字节,现在 16 字节整数大小并非闻所未闻。您可以通过使用 ansi 标准名称 int32_t 或 int64_t 来缓解此问题。

版本控制是您自己解决的问题。当您决定需要一个额外的字段(例如middleInitial)时,6 个月后会发生什么?您的新程序将如何读取旧数据文件及其新文件?如果您的旧程序遇到新数据,它如何知道拒绝它?

【讨论】:

  • 这是我的学期项目我绝对应该这样做但我无法解决问题我尝试不同的方法但我找不到问题
【解决方案2】:

尝试使用 .dat 文件。您可以编写和阅读整个结构。 https://www.google.com/amp/s/www.geeksforgeeks.org/readwrite-structure-file-c/amp/

【讨论】:

  • 请在此处提供答案,链接可能会过期... 引用链接是个好主意,但请将其视为参考,而不是答案的一部分。我建议不要使用 google 重定向表单,而是使用直接链接。
猜你喜欢
  • 2016-09-04
  • 1970-01-01
  • 2018-05-16
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
  • 2015-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多