有一磁盘文件“employee”,内存放职工的数据。每个职工的数据包括职工姓名、职
工号、性别、年龄、住址、工资、健康状况、文化程度。今要求将职工名、工资的信息单独抽出来另建一个简明的职工工资文件。
#include<stdio.h> #include<stdlib.h> #include <string.h> struct emploee {char num[6]: char name[10]: char sex[2]; int age; char addr[20]; int salary; char health[8]; char clas[10]; }em[10]; struct emp {char name[10]; int salary; }em_case[10]; int main() {FILE*fp1,*fp2; int i,j: if((fp1=fopen("emploee","r'))==NULL) {printf("can not open file.\n”); exit(0); } printf("\n NO. name sex age addr salary health class\n"); for(i=0;fread(&em[i],sizeof( struct emploee),1,fp1)!=0;i++) {printf("\n%4s%8s%4s%6d%10s%6d%10s%8s",em[i].num,em[i].name,em[i].sex, em[i].age,em[i].addr,em[i].salary,em[i].health,em[i].class); strcpy(em_case[i].name,em[i].name); em_case[i].salary=em[i].salary; } printf("\n\n ******************************* “); if((fp2=fopen("emp_salary","wb"))==NULL) {printf("can not open file\n"); exit(0); } for (j=0;j<i;j++) {if(fwrite(&em_case[j],sizeof(struct emp),1,fp2)!=1) printf("error!"); printf("\n %12s%10d",em_case[j].name,em_case[j].salary); } printf("\n **************************** “); fclose(fp1); fclose(fp2); return 0; }
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
struct emploee
{char num[6]:
char name[10]:
char sex[2];
int age;
char addr[20];
int salary;
char health[8];
char clas[10];
}em[10];
struct emp
{char name[10];
int salary;
}em_case[10];
int main()
{FILE*fp1,*fp2;
int i,j:
if((fp1=fopen("emploee","r'))==NULL)
{printf("can not open file.\n”);
exit(0);
}
printf("\n NO. name sex age addr salary health class\n");
for(i=0;fread(&em[i],sizeof( struct emploee),1,fp1)!=0;i++)
{printf("\n%4s%8s%4s%6d%10s%6d%10s%8s",em[i].num,em[i].name,em[i].sex,
em[i].age,em[i].addr,em[i].salary,em[i].health,em[i].class);
strcpy(em_case[i].name,em[i].name);
em_case[i].salary=em[i].salary;
printf("\n\n ******************************* “);
if((fp2=fopen("emp_salary","wb"))==NULL)
{printf("can not open file\n");
for (j=0;j<i;j++)
{if(fwrite(&em_case[j],sizeof(struct emp),1,fp2)!=1)
printf("error!");
printf("\n %12s%10d",em_case[j].name,em_case[j].salary);
printf("\n **************************** “);
fclose(fp1);
fclose(fp2);
return 0;
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题