代码:
struct subscriber
{
char phonenumber[20];
char name[50];
float amount;
}s;
void addrecords()
{
FILE *f;
char test;
f=fopen("file.txt","ab+");
//if(f==0)
//{ f=fopen("file.txt","wb+");
//system("clear");
//printf("please wait while we configure your computer");
//printf("/npress any key to continue");
//getchar();
//getch();
//}
while(1)
{
//system("clear");
printf("\n Enter phone number:");
scanf("%s",s.phonenumber);
printf("\n Enter name:");
fflush(stdin);
//scanf("%[^\n]",s.name);
scanf("%s",s.name);
printf("\n Enter amount:");
scanf("%f",&s.amount);
printf("check 1");
fwrite(&s,sizeof(s),1,f);
fflush(stdin);
printf("check 2");
//system("clear");
printf("1 record successfully added");
printf("\n Press esc key to exit, any other key to add other record:");
test=getchar();
//test=getche();
if(test==27)
break;
}
fclose(f);
}
我在这里面临两个问题:
- 尽管正在创建文件,但没有任何内容写入文件。
- 在我输入金额后,它会输出所有打印语句并转到 while 循环的初始位置,即它不会在 getchar 处等待。