0

How can I read these from a file and store it in a structure array?

struct bankInfo{
    char name[50];
    int pin;
    float balance;
    int loan[3];
    int loan_Total;
    int loan_count;
    char loan_reason[3][1000];
    float creditScore;

}customer[MAX_ACCOUNTS],closed_customers[MAX_ACCOUNTS];

Items to read and store in stucture:

Dam Maxim
123456
1000.0
1200 500 5000
3
bills school vacation
6700
425.0

Frank Silo
123456
1000.0
0 0 0
0
0 0 0
0
425.0

Grey Drun
123456
1000.0
0 0 0
0
0 0 0
0
425.0

the format when I stored it in the file is Name PIN Balance Loans Number of loans Reason For Loan Total amount of loans Credit Score

When there is no loan taken I just put 0 in

I tried reading the file using a while loop and storing it in the structure using fscanf but it seems like only the first info gets stored and not the others

fptr = fopen("BankManagement.txt","r");
    if(fptr == NULL){
        fclose(fptr);
        fptr = fopen("BankManagement.txt","w");
    }
    else{
        while(!feof(fptr)){
            fgets(str,100,fptr);
            str[strcspn(str, "\n")] = '\0';
            strcpy(customer[acc_count].name, str);
            fscanf(fptr,"%d",&customer[acc_count].pin);
            fscanf(fptr,"%f",customer[acc_count].balance);
            fscanf(fptr,"%d %d %d",&customer[acc_count].loan[0],&customer[acc_count].loan[acc_count],&customer[acc_count].loan[2]);
            fscanf(fptr,"%d",&customer[acc_count].loan_count);
            fscanf(fptr,"%s",&customer[acc_count].loan_reason[0], &customer[acc_count].loan_reason[1], &customer[acc_count].loan_reason[2]);
            fscanf(fptr,"%d",&customer[acc_count].loan_Total);
            fscanf(fptr,"%f",&customer[acc_count].creditScore);
            acc_count++;
        }
    }

I expected that all info in the structure would be stored

Kookaburra
  • 23
  • 3
  • 1
    `if (fptr == NULL) fclose (fptr)` ===> Why would you pass a `NULL` pointer to `fclose()`? Why would you not check the return value in that subsequent call to `fopen()`? Were you perhaps trying to check if the file doesn't already exist? – Harith May 20 '23 at 10:15
  • Checking the return of `fgets()` and `fscanf()` would help too. See https://stackoverflow.com/q/5431941/20017547 – Harith May 20 '23 at 10:18
  • https://stackoverflow.com/questions/16922871/why-glibcs-fclosenull-cause-segmentation-fault-instead-of-returning-error – William Pursell May 20 '23 at 12:00
  • https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong – William Pursell May 20 '23 at 12:01
  • You have a call to `fscanf` in which the format string is `"%s"`, but you are passing 3 other arguments. With a single conversion specifier, you cannot possibly read all 3 values for `loan_reason`. You compiler should warn you about this, and you should heed its warning. – William Pursell May 20 '23 at 12:06
  • `fgets()` reads the next line of the file. One line. You're using the same line to assign values to each member of the struct, while one line has the value of only one member, You must count lines to deduce which member it represents, and reset the count on empty lines. – Ale May 22 '23 at 11:30

0 Answers0