#1396. 职工名和工资信息-课后习题10.9

职工名和工资信息-课后习题10.9

问题描述

输入职工的数据,每个职工的数据包括职工姓名、职工号、性别、年龄、住址、工资、健康状况、文化程度。今要求将职工名、工资的信息单独抽出来另建一个简明的职工工资文件。

格式

输入

输入的格式

输出

输出的格式

样例

101 Li   m   23  Beijing  670  good F.H.D
102 Wang f   45  Shanghai 780  bad  master
103 Ma   f   32  Taijin   650  good  univ.
104 Liu  f   56  Xian     540  pass college
NO. name sex age addr salary health class
101 Li   m   23  Beijing  670  good F.H.D
102 Wang f   45  Shanghai 780  bad  master
103 Ma   f   32  Taijin   650  good  univ.
104 Liu  f   56  Xian     540  pass college
 ******************************
        Li      670
      Wang      780
        Ma      650
       Liu      540
  ******************************* press any key to continue.

题解

#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    class[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;
 }


提示

数据范围、额外说明、样例说明等等