回复 2楼 zklhp
这是一个添加函数中的一句,运行结果正确,应该没有错……
原程序如下:
void add()
{
FILE *fp;
int i=0;
if ((fp = fopen("number.dat", "rb+")) == NULL)
{
printf("Can't open file\n");
exit(0);
}
fread(&number,sizeof(int),1,fp);//从文件指针位置读取
fclose(fp);
if ((fp = fopen("employee.dat", "rb+")) == NULL)
{
printf("Can't open file\n");
exit(0);
}
fread(emp,sizeof(struct employee),number,fp);
fclose(fp);
if ((fp = fopen("employee.dat", "wb+")) == NULL)
{
printf("Can't open file\n");
exit(0);
}
printf("
编号
姓名
性别
出生日期
职称
工作年限\n");
scanf("
%s
%s
%s
%s
%s
%d",emp[number].num,emp[number].name,emp[number].sex,emp[number].shengri,emp[number].degree,&emp[number].nianxian);
number++;
sort();
fwrite(emp,sizeof(struct employee),number, fp);
fclose(fp);
if ((fp = fopen("number.dat", "wb+")) == NULL)
{
printf("Can't open file\n");
exit(0);
}
fwrite(&number,sizeof(int),1,fp);
fclose(fp);
if ((fp = fopen("employee.dat", "rb+")) == NULL)
{
printf("Can't open file\n");
exit(0);
}
printf(" 添加成功!!\n");
printf("
编号
姓名
性别
出生日期
职称
工作年限\n");
for (i = 0; fread(&emp[i],sizeof(struct employee),1,fp) == 1 ; i++)
{
printf("
%s
%s
%s
%s
%s
%d\n",emp[i].num,emp[i].name,emp[i].sex,emp[i].shengri,emp[i].degree,emp[i].nianxian);
}
fclose(fp);
}