Exciless
Exciless

Reputation: 1

How Can I Read and Write User Information to file with structure

I Create file and I want to get information from users and write in file or read informations from data ? I want to use in Switch

The code I use to write data to a file: fwrite(&x, sizeof(struct userRec), 1, fout); fclose(fout);

The code I use to write data to a file: fread(&x, sizeof(struct userRec), 1, fin);

When I try to write some information on file and I open with notepad, I see just some NULL characters.

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);

    }
}

Upvotes: 0

Views: 120

Answers (2)

Gem Taylor
Gem Taylor

Reputation: 5613

You certainly can do this, but do you really want to?

Main disadvantages are to do with portability of the file: endians, alignments, processor size, and versions

The endian problem is that a number type that stores more than 128 or 256 needs to use multiple bytes, and there are two main schemas depending whether the first byte stores the highest part of the number (big endian) or the lowest part (little endian). If a file is stored in one form on one computer, then it can't be correctly read back on a computer configured the other way. Much of the world is running on Intel or little-endian mode ARM, so it may seem that little endian has won, but it may not always be the case.

Alignments are where the compiler aligns each of the fields of your structure to provide better performance, leaving the odd spare byte. Different processors have different alignment rules, so again the file may be misread on a different processor, or even the same platform with your program built with slightly different options.

The processor size is an issue that the default size of an integer has grown over the years. Had you saved your file in 1980, your int might have only taken up 2 bytes, as that was the processor int size back then. In steps it has grown to 4 bytes, 8 bytes, and now 16 byte integer size is not unheard of. You can mitigate this issue by using the ansi standard names int32_t or int64_t.

Versioning is a problem you make for yourself. What happens in 6 months time when you decide you need an extra field, say middleInitial? How will your new program be able to read your old data files as well as its new files? How will your old program know to reject the new data if it encounters it?

Upvotes: 1

skowronsky98
skowronsky98

Reputation: 1

Try to use .dat file. You can write and read whole structure. https://www.google.com/amp/s/www.geeksforgeeks.org/readwrite-structure-file-c/amp/

Upvotes: 0

Related Questions