任何人都可以请帮我纠正下面的代码吗?函数“fwrite”写入意外的记录
int main()
{
FILE *fp;
char another='Y';
struct emp
{
char name[40];
int age;
float bs; //basic salary
}e;
fp=fopen("Employee.DAT","wb");
if(fp==NULL)
{
printf("Cannot open file");
exit(1);
}
while(another=='Y')
{
printf("\nEnter name, age and basic salary:");
scanf("%s %d %f",e.name,&e.age,&e.bs);
fwrite(&e,sizeof(e),1,fp);
printf("Add another record?(Y/N)");
fflush(stdin);
another=getche();
}
fclose(fp);
return 0;
}
我想以下记录文件“Employee.DAT”输入:
Abc 19 12345 Def 20 12345
但我得到的文件“Employee.DAT”在预期的输出如下安装在图像:
我认为问题是与“fwrite”,但我无法找出问题。
*您如何*尝试查看文件?你知道它是*二进制*,通常不可读为文本文件?它是二进制的,意味着你写的数据是用计算机使用的内部二进制格式写的,你不能简单地在文本文件中打开文件,并希望看到你编写的文本为文本。 –
顺便说一句,不要做'fflush(stdin)',它不被C标准支持。 –
@JoachimPileborg文件在我保存程序的文件夹中创建。 –