如何读取和写入具有结构的文件的用户信息

问题描述 投票:0回答:2

我创建文件,我想从用户那里获取信息并写入文件或从数据中读取信息?我想在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);

    }
}
c file structure
2个回答
1
投票

你当然可以做到这一点,但你真的想要吗?

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

端序问题是存储超过128或256的数字类型需要使用多个字节,并且有两个主要模式,这取决于第一个字节是存储数字的最高部分(大端)还是最低部分(小端) )。如果文件以一种形式存储在一台计算机上,则无法在另一种方式配置的计算机上正确读回。世界上大部分地区都运行在英特尔或小端模式ARM上,所以似乎小端已经赢了,但情况可能并非总是如此。

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

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

版本控制是您自己的问题。 middleInitial说,当你决定需要一个额外的领域时,6个月内会发生什么?您的新程序如何能够读取旧数据文件及其新文件?如果遇到新数据,您的旧程序将如何知道拒绝新数据?


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.