程序代码:
#include<stdio.h>
#include<string.h>
struct Date
{
char Month[3];
char Day[3];
char Year[5];
};
struct Staff
{
char No[6];/*职工号*/
char Name[10];/*姓名*/
int Sex;/*性别*/
struct Date Birthday;/*出生年月*/
char Education[10];/*学历*/
char Duty[10];/*职务*/
int Salary;/*工资*/
char Address[10];/*住址*/
char Tel[12];/*电话*/
/*其他*/
}St[2000];
int initial()/*读文件记录到数组,同时记录文件记录个数*/
{
int i=0,j;
FILE *fp;
if((fp=fopen("St.dat","rb"))==NULL)
{
printf("不能打开文件\n");
exit(0);
}
i=0;
while(1)
{
fread(&St[i],sizeof(struct Staff),1,fp);
if(!feof(fp))
{
/*printf("%10d%10s%2d%5d%3d%3d%10s%10s%8s%10s%15s",St[i].No,St[i].Name,St[i].Sex,
St[i].Birthday.Year,St[i].Birthday.Month,St[i].Birthday.Day,
St[i].Education,St[i].Duty,St[i].Salary,St[i].Address,St[i].Tel);*/
i++;
}
else break;
}
fclose(fp);
return(i);
}
int save(int g)
{
int i;
FILE *fp;
if((fp=fopen("St.dat","wb")==NULL))
{
printf("无法打开此文件!\n");
return;
}
for(i=0;i<g;i++)
printf("%-6s%-10s%-2d%-5s%-3s%-3s%-10s%-10s%-7d%-10s%-12s\n",St[i].No,St[i].Name,St[i].Sex,
St[i].Birthday.Year,St[i].Birthday.Month,St[i].Birthday.Day,
St[i].Education,St[i].Duty,St[i].Salary,St[i].Address,St[i].Tel);
/*for(i=0;i<g;i++)
{
if(fwrite(&St[i],sizeof(struct Staff),1,fp)!=1)
printf("文件出错!");
}
fclose(fp);*/
for(i=0;i<g;i++)
{
if(fwrite(&St[i],sizeof(struct Staff),1,fp)!=1)////////我会出问题!!!!!!
printf("文件出错!\n");
}
fclose(fp);
}
int main()
{
int k;
k=initial();
save(k);
}
这是一段测试代码,还是在同样的地方出问题。
[此贴子已经被作者于2017-6-13 10:46编辑过]