琥珀玲珑
琥珀玲珑的世界,也是你们的世界哦。大家一起来吧!!!!

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#define size 3

struct employee                      

{  char name[6];                           //姓名  

char num[8];                            //编号  

char sex[2];                           //性别

 int age;                                //年龄  

char addr[10];                          //地址  

int money;                             //工资  

char health[4];                         //身体健康程度  

char cul[4];                           //文化程度

}emp[size];                                               //employee结构体

struct employee_type

{  char name[6];  

int money;

}empl[size];                                              //employee_type结构体

void save()                                                             //存储职工的全部信息

{  FILE *fp;

 int i;  

printf("Please enter data of employee:\n");

 for(i=0;i<size;i++)  

 scanf("%s%s%s%d%s%d%s%s",emp[i].name,emp[i].num,emp[i].sex,&emp[i].age,emp[i].addr,&emp[i].money,emp[i].health,emp[i].cul);

 if((fp=fopen("employee.dat","wb"))==NULL)  {   printf("can't open file!\n");   exit(0);  }  

for(i=0;i<size;i++)  

 if(fwrite(&emp[i],sizeof(struct employee),1,fp)!=1)         //从emp[i]地址开始的数据给fp文件    

printf("file write error!\n");  

 fclose(fp); }

void duchu()                                                           //读出职工的全部信息

{  FILE *fp;  int i;  printf("显示职工信息:\n");  

if((fp=fopen("employee.dat","rb"))==NULL)

 {   printf("can't open file!\n");   exit(0);  }  

for(i=0;i<size;i++)  

{   fread(&emp[i],sizeof(struct employee),1,fp);                 //把存放文件fp的地址给emp[i]   

printf("%-5s %-5s %-5s %-5d %-5s %-5d %-5s %-5s\n",emp[i].name,emp[i].num,emp[i].sex,emp[i].age,emp[i].addr,emp[i].money,emp[i].health,emp[i].cul);  }  fclose(fp); }

void chouqu()                                                   //抽取职工的name及money

{  FILE *fp,*in;  int i;

 if((fp=fopen("employee.dat","rb"))==NULL)                     //以只读的方式打开文件

 {   printf("can't open file!\n");   exit(0);  }  

if((in=fopen("employee_type.dat","wb"))==NULL)          //以只读的方式打开文件  

{   printf("can't open file!\n");   exit(0);  }

 for(i=0;i<size;i++)

 {   fread(&emp[i],sizeof(struct employee),1,fp);     

strcpy(empl[i].name,emp[i].name);     

empl[i].money=emp[i].money;   

if(fwrite(&empl[i],sizeof(struct employee_type),1,in)!=1)            //从empl[i]的地址开始的数据给in文件    

printf("file write error!\n");  }  fclose(fp);  fclose(in); }

void duchu2()                                                                   //读出抽取出来的信息

{  FILE *in;  int i;  printf("显示信息:\n");

 if((in=fopen("employee_type.dat","rb"))==NULL)                                //以只读的方式打开文件  

{   printf("can't open file!\n");   exit(0);  }

 for(i=0;i<size;i++)

 {   fread(&empl[i],sizeof(struct employee_type),1,in);                  //把文件in内的数据地址给empl[i]   

printf("%-5s %-5d\n",empl[i].name,empl[i].money);  }  fclose(in); }     

int main()

{  //save();  chouqu();  duchu();  duchu2();  return 0; }

 

 

 

posted on 2013-07-16 10:35  琥珀玲珑  阅读(415)  评论(0编辑  收藏  举报