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;
在这个程序中,我试图将记录写入名为employee.dat 的文件中。程序执行得很好,但只需要一条员工记录,然后程序就会终止。它不要求添加下一条记录,即
fflush(stdin);
scanf("%c", &another);
没有在程序中执行。
提前致谢....