9.有一磁盘文件employee,内存放职工的数据。每个职工的数据包括职工姓名、职工号、性别、年龄、住址、工资、健康状况、文化程度。今要求将职工名、工资的信息单独抽出来另建一个简明的职工工资文件。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 3
struct Employee
{
char name[10];
int num;
char sex[5];
int age;
char adress[10];
int salary;
char health[10];
char edu[10];
}emp1[SIZE];
struct Wage
{
char name[10];
int salary;
}emp2[SIZE];
void save1()
{
FILE *fp;
int i;
if((fp=fopen("employee.dat","wb"))==NULL)
{
printf("can't open file!\n");
exit(0);
}
for(i=0;i<SIZE;i++)
{
if(fwrite(&emp1[i],sizeof(struct Employee),1,fp)!=1)
printf("file write error\n");
}
fclose(fp);
}
void save2()
{
FILE *fp;
int i;
if((fp=fopen("wage.dat","wb"))==NULL)
{
printf("can't open file!\n");
exit(0);
}
for(i=0;i<SIZE;i++)
{
if(fwrite(&emp2[i],sizeof(struct Wage),1,fp)!=1)
printf("file write error\n");
}
fclose(fp);
}
int main()
{
FILE *fp;
int i;
for(i=0;i<SIZE;i++)
{
printf("请输入第%d个职工的姓名、职工号、性别、年龄、住址、工资、健康状况、文化程度:",i+1);
scanf("%s%d%s%d%s%d%s%s",emp1[i].name,&emp1[i].num,emp1[i].sex,&emp1[i].age,emp1[i].adress,&emp1[i].salary,emp1[i].edu,emp1[i].health);
printf("第%d个职工的姓名、职工号、性别、年龄、住址、工资、健康状况、文化程度为:",i+1);
printf("%s %d %s %d %s %d %s %s\n\n",emp1[i].name,emp1[i].num,emp1[i].sex,emp1[i].age,emp1[i].adress,emp1[i].salary,emp1[i].health,emp1[i].edu);
}
save1();
fp=fopen("employee.dat","rb");
for(i=0;i<SIZE;i++)
{
fread(&emp1[i],sizeof(struct Employee),1,fp);
strcpy(emp2[i].name,emp1[i].name);
emp2[i].salary=emp1[i].salary;
}
fclose(fp);
save2();
fp=fopen("wage.dat","rb");
for(i=0;i<SIZE;i++)//为了验证磁盘文件Wage中是否已存在此数据,用for语句从Wage文件中读入数据到emp2数组,然后向屏幕上输出
{
fread(&emp2[i],sizeof(struct Wage),1,fp);
printf("第%d个职工的姓名和工资为:",i+1);
printf("%s %d\n",emp2[i].name,emp2[i].salary);
}
fclose(fp);
return 0;
}
在VS2019下,需将源文件的fopen和scanf和strcpy做些修改:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 3
struct Employee
{
char name[10];
int num;
char sex[5];
int age;
char adress[10];
int salary;
char health[10];
char edu[10];
}emp1[SIZE];
struct Wage
{
char name[10];
int salary;
}emp2[SIZE];
void save1()
{
FILE* fp;
int i;
fopen_s(&fp, "employee.dat", "wb");
if (fp == NULL)
{
printf("can't open file!\n");
exit(0);
}
for (i = 0; i < SIZE; i++)
{
if (fwrite(&emp1[i], sizeof(struct Employee), 1, fp) != 1)
printf("file write error\n");
}
fclose(fp);
}
void save2()
{
FILE* fp;
int i;
fopen_s(&fp, "wage.dat", "wb");
if (fp == NULL)
{
printf("can't open file!\n");
exit(0);
}
for (i = 0; i < SIZE; i++)
{
if (fwrite(&emp2[i], sizeof(struct Wage), 1, fp) != 1)
printf("file write error\n");
}
fclose(fp);
}
int main()
{
FILE* fp;
int i;
for (i = 0; i < SIZE; i++)
{
printf("请输入第%d个职工的姓名、职工号、性别、年龄、住址、工资、健康状况、文化程度:", i + 1);
scanf_s("%s%d%s%d%s%d%s%s", emp1[i].name, (unsigned int)sizeof(emp1[i].name), &emp1[i].num, emp1[i].sex, (unsigned int)sizeof(emp1[i].sex), &emp1[i].age, emp1[i].adress, (unsigned int)sizeof(emp1[i].adress), &emp1[i].salary, emp1[i].edu, (unsigned int)sizeof(emp1[i].edu), emp1[i].health, (unsigned int)sizeof(emp1[i].health));
printf("第%d个职工的姓名、职工号、性别、年龄、住址、工资、健康状况、文化程度为:", i + 1);
printf("%s %d %s %d %s %d %s %s\n\n", emp1[i].name, emp1[i].num, emp1[i].sex, emp1[i].age, emp1[i].adress, emp1[i].salary, emp1[i].health, emp1[i].edu);
}
save1();
fopen_s(&fp, "employee.dat", "rb");
for (i = 0; i < SIZE; i++)
{
fread(&emp1[i], sizeof(struct Employee), 1, fp);
strcpy_s(emp2[i].name, strlen(emp1[i].name) + 1, emp1[i].name);
emp2[i].salary = emp1[i].salary;
}
fclose(fp);
save2();
fopen_s(&fp, "wage.dat", "rb");
for (i = 0; i < SIZE; i++)//为了验证磁盘文件Wage中是否已存在此数据,用for语句从Wage文件中读入数据到emp2数组,然后向屏幕上输出
{
fread(&emp2[i], sizeof(struct Wage), 1, fp);
printf("第%d个职工的姓名和工资为:", i + 1);
printf("%s %d\n", emp2[i].name, emp2[i].salary);
}
fclose(fp);
return 0;
}