C “是否有替代方案?”;do… ;而(!feof(filepointer))&引用;

C “是否有替代方案?”;do… ;而(!feof(filepointer))&引用;,c,C,这是一个新代码除了第一个idk之外,它不会识别文件中的其他帐户有什么问题吗?我熬夜了,顺便说一句,我使用了你的一种方法@chux,它以前工作过,所以我有点困惑为什么现在不工作 void make_deposit(void) //function make deposit to an account { float deposit=0; display_logo(); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13); pr

这是一个新代码除了第一个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");
 }

}
void make\u存款(void)//函数将存款存入帐户
{
浮动存款=0;
显示_徽标();
SetConsoleTextAttribute(GetStdHandle(标准输出句柄),13);
printf(“\n\n存储处理在此完成\n\n”);
int id_two=0;
printf(“请输入您的帐号”\n);
scanf(“%d”和&id_-two);
系统(“cls”);
if((acc=fopen(“Active Accounts.txt”,“r”))==NULL)
{
printf(“\n打开文件时出错”);
printf(“按回车键继续”);
getch();
操作主菜单();
}
int-cnt;
而((cnt=fscanf(acc,“%d%s%s%d%d%d%s%f”),&students.account\u num,
学生的名字,
students.lastname、students.parish和students.yob和students.age\u计算,
&students.payment\u type、students.password和students.acc\u bal))==9)
{
如果(id\u two==学生帐户\u num)
{
SetConsoleTextAttribute(GetStdHandle(标准输出句柄),10);
printf(“找到帐户!”);
printf(“\n======================================================================
==========");
printf(“>>帐户:%d\n”,students.Account\u num);
printf(“>>名字:%s\n”,students.firstname);
printf(“>>姓:%s\n”,students.lastname);
printf(“>>教区:%s\n”,students.Parish);
printf(“>>出生年份:%d\n”,students.yob);
printf(“>>年龄:%d\n”,学生.Age\u计算);
printf(“>>储蓄期:%d年,\n”,学生。付款类型);
printf(“>>密码:%s\n”,students.Password);
printf(“>>账户余额:$%.2f\n”,students.acc\u bal);
printf(“=====================================================
=====================================\n“;
存款:
printf(“输入您希望存入的金额\n”);
scanf(“%f”和存款);
如果(存款=0)
{
如果((acc=fopen(“Active Accounts.txt”,“r+”)==NULL)
{
printf(“\n打开文件时出错”);
printf(“按回车键继续”);
getch();
操作主菜单();
}
students.acc_bal=students.acc_bal+押金;
fprintf(acc,“%d\n”,学生帐户号);
fprintf(acc,“%s\n”,学生名);
fprintf(acc,“%s\n”,students.lastname);
fprintf(acc,“%s\n”,学生。教区);
fprintf(acc,“%d\n”,students.yob);
fprintf(acc,“%d\n”,学生年龄计算);
fprintf(acc,“%d\n”,学生。付款类型);
fprintf(acc,“%s\n”,学生密码);
fprintf(acc,“%f\n”,students.acc\u bal);
fseek(acc,0,SEEK_SET);
printf(“存款成功\n”);
printf(“您的账户余额为%.2f\n美元”,students.acc\u bal);
printf(“按enter键继续\n”);
getch();
操作主菜单();
fclose(acc);
}
}
其他的
{
printf(“没有该号码的现有帐户”);
printf(“按enter键返回主菜单\n”);
getch();
操作主菜单();
}
}
如果(cnt!=EOF)
{
printf(“存在文件错误\n”);
}
否则,如果(费罗(acc))
{
printf(“发生文件错误\n”);
}
其他的
{
printf(“文件错误\n”);
}
}

使用
fscanf()
中的返回值

或者更简单地说

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);
}
[编辑] 或


您必须单独检查每个
fscanf()
操作,如果这样做,
feof()
将变得多余
while(fscanf(fptr,“%d%s”,&employee.account\u num,employee.firstname)==2)
{…打印有效数据…}
。使用在顶部测试的循环和*始终*(但**始终**)测试输入操作的返回,因为这是检测EOF的原因。使用
feof()`仅在检测到故障后区分I/O错误和EOF。好吧,它没有使用feof(),但它没有赢得任何优雅的分数。如果我不知道文件停止在哪里,我如何确定for循环的结束点(我使用的是y,而:(我不知道边界)@user3293168如果(cnt==EOF)中断,在遇到文件结尾时退出循环。@user3293168如果
cnt==EOF
出现文件结尾(很可能)或I/O错误(罕见)。如果代码在收到
cnt==EOF
后需要区分,请使用
feof(fptr)
feror(fptr)
进行区分。我指的是代码中的FOR循环“FOR(;)”,计数器需要有一个起始值和结束值,比如FOR(x=1;x)
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();