int main()
{
FILE *fp;
char another = 'Y';
struct emp
{
char name[20];
int age;
float bs;
};
struct emp e;
fp = fopen("employee.dat", "w");
if(fp == NULL)
{
printf("file cannot be opened for writing\n");
exit(1);
}
while(another == 'Y')
{
printf("\n enter name, age and basic salary: ");
scanf("%s %d %f", e.name, &e.age, &e.bs);
fprintf(fp, "%s %d %f\n", e.name, e.age, e.bs);
printf(" Add another record (Y/N)");
fflush(stdin);
scanf("%c", &another);
}
fclose(fp);
return 0;
在这个程序中,我试图将记录写入文件named,employee.dat。程序执行得很好,但只需要一条员工记录,然后程序终止。它不要求下一条记录添加,即,将记录写入文件:c
fflush(stdin);
scanf("%c", &another);
没有在程序中执行。
在此先感谢....
没有必要! noly使用flushall(); scanf后 –
@OneManCrew你是什么意思“不需要”?这是另一种方式,与您的一样好。 –