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