有没有替代“do ... while(!feof(file pointer));”

时间:2014-02-28 17:29:15

标签: c

这是一个新的代码,它不会识别文件中的其他帐户,而不是第一个idk什么是错的,我熬夜了顺便说一句,我使用了你的一个方法@chux它之前工作所以我有点困惑为什么现在不是

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

}

1 个答案:

答案 0 :(得分:0)

使用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);
}

或更简单

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

[编辑] 或

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