69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
typedef struct staff{
int number; //号码
char name[8]; //姓名
char sex[4]; //性别
int age; //年龄
char abode[20]; //住宅
int wage; //工资
char health[8]; //健康
char culture[8]; //文化
}ff;
int main()
{
FILE *fp,*fp1;
ff jg[5];
int i=0;
fp=fopen("employee.txt","r");
if (!fp) {
fprintf(stderr, "Open file error!\n");
return -1;
}
fp1=fopen("employee1.txt","w");
if (!fp1) {
fprintf(stderr, "Open error!");
return -1;
}
fscanf(fp,"%d %s %s %d %s %d %s %s\n",&jg[i].number,jg[i].name,jg[i].sex,&jg[i].age,jg[i].abode,&jg[i].wage,jg[i].health,jg[i].culture);
while(!feof(fp)){
fprintf(fp1,"%d %s %d\n",jg[i].number,jg[i].name,jg[i].wage);
i++;
fscanf(fp,"%d %s %s %d %s %d %s %s\n",&jg[i].number,jg[i].name,jg[i].sex,&jg[i].age,jg[i].abode,&jg[i].wage,jg[i].health,jg[i].culture);
}
fclose(fp);
fclose(fp1);
return 0;
}
fp1=fopen("employee1.txt","w"); //打开方式为w,这种方式当文件再次打开时,会清除掉之前的内容,宜换成a+方式
具体文件打开方式的区别,可参考:
http://no001.blog.51cto.com/1142339/526653