コード:
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);
}
ここで2つの問題に直面しています:
- ファイルが作成されていますが、ファイルに何も書き込まれていません。
- 量を入力すると、すべての print ステートメントが出力され、getchar で待機しない while ループの最初に移動します。