1

This is a new code it wont recognize the other accounts in the file other than the first one idk whats wrong with it i stayed up all night and btw i used one of your methods @chux it was working before so im kinda confused why it isnt now

void make_deposit(void)  //function make deposit to an account
{
float deposit=0;
display_logo();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
printf("\n\nDEPOSIT PROCESSING IS DONE HERE\n\n");
 int id_two=0;
printf("Please enter your account number\n");
scanf("%d",&id_two);
system("cls");
        if((acc=fopen("Active Accounts.txt","r"))==NULL)
     {
        printf("\nERROR OPENING FILE.");
        printf("Press enter to continue");
        getch();
        action_main_menu();
     }

int cnt;
while ((cnt =  fscanf(acc,"%d%s%s%s%d%d%d%s%f",&students.account_num,
students.firstname,
students.lastname,students.parish,&students.yob,&students.age_calculation,
&students.payment_type,students.password,&students.acc_bal)) == 9)
{
    if(id_two==students.account_num)
   {
       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
printf("ACCOUNT FOUND!");
 printf("\n======================================================================
==========");
printf("                 >> Account #       : %d\n",students.account_num);
printf("                 >> First Name      : %s\n",students.firstname);
printf("                 >> Last Name       : %s\n",students.lastname);
printf("                 >> Parish          : %s\n",students.parish);
printf("                 >> Year of Birth   : %d\n",students.yob);
printf("                 >> Age             : %d\n",students.age_calculation);
printf("                 >> Savings Period  : %d year(s)\n",students.payment_type);
printf("                 >> Password        : %s\n",students.password);
printf("                 >> Account balance : $%.2f\n",students.acc_bal);
printf("=====================================================
===========================\n");
deposits:
printf("Enter the amount of money you wish to deposit\n");
scanf("%f",&deposit);
if(deposit<=0)
{
    printf("Were very sorry but you cannot deposit $0 or less\n");
    printf("Press enter to re-enter the deposit amount\n");
    getch();
    goto deposits;
}
else if(deposit>=0)
{
    if((acc=fopen("Active Accounts.txt","r+"))==NULL)
     {
        printf("\nERROR OPENING FILE.");
        printf("Press enter to continue");
        getch();
        action_main_menu();
     }
students.acc_bal=students.acc_bal+deposit;
fprintf(acc,"%d\n",students.account_num);
fprintf(acc,"%s\n",students.firstname);
fprintf(acc,"%s\n",students.lastname);
fprintf(acc,"%s\n",students.parish);
fprintf(acc,"%d\n",students.yob);
fprintf(acc,"%d\n",students.age_calculation);
fprintf(acc,"%d\n",students.payment_type);
fprintf(acc,"%s\n",students.password);
fprintf(acc,"%f\n",students.acc_bal);
fseek(acc, 0, SEEK_SET);
    printf("DEPOSIT WAS SUCCESSFUL\n");
    printf("YOUR ACCOUNT BALANCE IS $%.2f\n",students.acc_bal);
    printf("Press enter to continue\n");
    getch();
    action_main_menu();
    fclose(acc);
 }
   }
 else
   {
    printf("THERE IS NO EXISTING ACCOUNT WITH THAT NUMBER\n");
    printf("Press enter to return to the main menu\n");
    getch();
    action_main_menu();
    }

 }
  if (cnt != EOF)
 {
    printf("THERE IS A FILE ERROR\n");
 }
 else if (ferror(acc))
 {
    printf("FILE ERROR OCCURED\n");
 }
 else
 {
     printf("FILE ERROR\n");
 }

}
C-learner
  • 31
  • 3
  • 1
    You have to check each of the `fscanf()` operations individually, and if you do that, the `feof()` becomes redundant. `while (fscanf(fptr, "%d %s", &employee.account_num, employee.firstname) == 2)` { …print valid data… }`. Use loops tested at the top and *always* (but **always**) test the return from the input operations since that is what detects EOF. Use `feof()` only to distinguish between an I/O error and EOF after you've detected a failure. – Jonathan Leffler Feb 28 '14 at 17:37

1 Answers1

0

Use the return value from fscanf().

for (;;) {
   int cnt = fscanf(fptr,"%d" ,&employee.account_num);
   if (cnt == EOF) break;
   if (cnt != 1) Handle_Error();
   cnt = fscanf(fptr,"%s", employee.firstname);
   if (cnt == EOF) break;
   if (cnt != 1) Handle_Error();
   printf("%d", employee.account_num);
   printf("%s", employee.firstname);
}

or more simply

for (;;) {
   int cnt = fscanf(fptr,"%d%s" ,&employee.account_num, employee.firstname);
   if (cnt == EOF) break;
   if (cnt != 2) Handle_Error();
   printf("%d",employee.account_num);
   printf("%s",employee.firstname);
}

[Edit] or

int cnt; 
while ((cnt = fscanf(fptr,"%d%s", 
    &employee.account_num, employee.firstname)) == 2) {
  printf("%d",employee.account_num);
  printf("%s",employee.firstname);  // post uses .account_num
}
if (cnt != EOF) Handle_Error();  // Only scanned a partial amount
else if (ferror(fptr)) Handle_IOError();
else Handle_NormalEndOfFile();
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 1
    Well, it does the job without using `feof()`, but it doesn't win any marks for elegance. – Jonathan Leffler Feb 28 '14 at 17:38
  • What if i dont know where my file stops how can i determine the ending point in my for loop(thats y i used do while :( i dont know the boundary) – C-learner Feb 28 '14 at 17:51
  • 1
    @user3293168 the `if (cnt == EOF) break;` exits the loop when end of file is encountered. – lurker Feb 28 '14 at 18:01
  • @user3293168 When `cnt == EOF` either the End-of-file occurred (most likely) or an I/O error occurred. (Rare). Should code need to differentiate, after receiving the `cnt == EOF`, use `feof(fptr)` or `ferror(fptr)` to distinguish. – chux - Reinstate Monica Feb 28 '14 at 18:05
  • I was referring to the FOR LOOP inside your code "for (;;)" there needs to be a starting and ending value for the counter like for(x=1; x<=(idk what ending value to put here) x++) – C-learner Feb 28 '14 at 18:21
  • @user3293168 Confident the is no "need" as you suggest. `for (;;)` compiles and means "forever". If you would like to handle the `EOF` and `cnt != expected)` outside the loop, use `int cnt; while ((cnt = fscanf(fptr,"%d%s" ,&employee.account_num, employee.firstname)) != 2) { ... } if (cnt != EOF) Handle_Error();`. – chux - Reinstate Monica Feb 28 '14 at 18:29
  • Ohh thanks guys i didnt know for(;;) was a valid line of code, thought you were typing short, <3 thanks (: – C-learner Feb 28 '14 at 18:37
  • Check out my new edit at the top..i didnt wanna make another post so its at the top im having a problem reading specific lines from the file see top of page – C-learner Mar 01 '14 at 22:02