0

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

    }
}
4

2 回答 2

1

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

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

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

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

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

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

于 2019-05-01T18:52:58.730 回答
0

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

于 2019-05-01T17:00:43.550 回答