当前位置:
首页 >
职工工资文件
发布时间:2025/3/15
30
豆豆
一.前言
C语言谭书中文件一章,不错的题目。
二.题目
9.有一磁盘文件employee,内存放职工的数据。每个职工的数据包括职工姓名、职工号、性别、年龄、住址、工资、健康状况、文化程度。今要求将职工名、工资的信息单独抽出来另建一个简明的职工工资文件。
由于一开始没有employee文件,需要自己创建一下:
#include<stdio.h> #include<stdlib.h> typedef struct{char name[20];//职工姓名int num;//职工号char gender[10];//性别int age;char address[30];//住址 int wage;char h_condition[10];//健康状况 char edu_degree[10];//文化程度 }Staff;int main(){//创造一个employee文件 Staff staffs[4];FILE*fp;int i;int count=0;Staff*p=staffs;if((fp=fopen("C:\\Users\\17604\\Desktop\\employee.dat","wb"))==NULL){printf("打开文件失败!!");exit(0); }//从键盘输入职工数据 for(p=staffs;p<staffs+4;p++){count++;printf("请输入第%d个职工的职工号,姓名,性别,年龄,住址,工资,健康状况,文化程度:\n",count);scanf("%d%s%s%d%s%d%s%s",&p->num,p->name,p->gender,&p->age,p->address,&p->wage,p->h_condition,p->edu_degree);} // fclose(fp);//打印职工数据printf("职工号 姓名 性别 年龄 住址 工资 健康状况 文化程度:\n");for(p=staffs;p<staffs+4;p++){printf("%-4d%8s%4s%6d%10s%6d%10s%10s\n",p->num,p->name,p->gender,p->age,p->address,p->wage,p->h_condition,p->edu_degree);} //写入 p=staffs;for(i=0;i<4;i++){if(fwrite(p,sizeof(Staff),1,fp)!=1){printf("写入文件失败!!\n");}p++; }fclose(fp);return 0; }程序执行效果图:(在我的计算机中C:\Users\17604\Desktop\employee.dat,文件就生成了)
可以开始干题目了!!!
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct{char name[20];//职工姓名int num;//职工号char gender[10];//性别int age;char address[30];//住址 int wage;char h_condition[10];//健康状况 char edu_degree[10];//文化程度 }Staff;typedef struct{char name[20];//职工姓名int wage; } StaffWage; int main(){Staff staffs[4];StaffWage staffwages[4];StaffWage*pt=staffwages;FILE*fp;int i;Staff*p=staffs;if((fp=fopen("C:\\Users\\17604\\Desktop\\employee.dat","rb"))==NULL){printf("打开文件失败!!");exit(0); }//读入 p=staffs;for(i=0;i<4;i++){if(fread(p,sizeof(Staff),1,fp)!=1){printf("读入文件失败!!\n");}p++; }fclose(fp);//打印职工数据同时给职工工资表赋值 printf("职工号 姓名 性别 年龄 住址 工资 健康状况 文化程度:\n");for(p=staffs;p<staffs+4;p++){printf("%-4d%8s%4s%6d%10s%6d%10s%10s\n",p->num,p->name,p->gender,p->age,p->address,p->wage,p->h_condition,p->edu_degree);strcpy(pt->name,p->name);pt->wage=p->wage;pt++; } //将职工工资表写入文件 if((fp=fopen("C:\\Users\\17604\\Desktop\\employeeWage.dat","wb"))==NULL){printf("打开文件失败!!");exit(0); }pt=staffwages;for(i=0;i<4;i++){if(fwrite(pt,sizeof(StaffWage),1,fp)!=1){printf("写入文件失败!!\n");}pt++; }fclose(fp);return 0; }效果图:
验证,由于是二进制文件,用记事本无法查看,所以写一个程序查看一下是否正确(是否为题目所求)
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct{char name[20];//职工姓名int num;//职工号char gender[10];//性别int age;char address[30];//住址 int wage;char h_condition[10];//健康状况 char edu_degree[10];//文化程度 }Staff;typedef struct{char name[20];//职工姓名int wage; } StaffWage; int main(){Staff staffs[4];StaffWage staffwages[4];StaffWage*pt=staffwages;FILE*fp;int i;Staff*p=staffs;if((fp=fopen("C:\\Users\\17604\\Desktop\\employee.dat","rb"))==NULL){printf("打开文件失败!!");exit(0); }//读入 p=staffs;for(i=0;i<4;i++){if(fread(p,sizeof(Staff),1,fp)!=1){printf("读入文件失败!!\n");}p++; }fclose(fp);//打印职工数据同时给职工工资表赋值 printf("职工号 姓名 性别 年龄 住址 工资 健康状况 文化程度:\n");for(p=staffs;p<staffs+4;p++){printf("%-4d%8s%4s%6d%10s%6d%10s%10s\n",p->num,p->name,p->gender,p->age,p->address,p->wage,p->h_condition,p->edu_degree);strcpy(pt->name,p->name);pt->wage=p->wage;pt++; } //将职工工资表写入文件 if((fp=fopen("C:\\Users\\17604\\Desktop\\employeeWage.dat","wb"))==NULL){printf("打开文件失败!!");exit(0); }pt=staffwages;for(i=0;i<4;i++){if(fwrite(pt,sizeof(StaffWage),1,fp)!=1){printf("写入文件失败!!\n");}pt++; }fclose(fp);return 0; }效果图:
附加题:
10.从第9题的“职工工资文件”中删去一个职工的数据,再存回原文件。
效果图:
再使用上述程序查看文件效果图:
后记:
由于今天时间有点匆忙,任务有点多,撸码,没有考虑很多细节。只是为完成和实现。代码写的有点死。很多代码块可以写成函数模块。
呃呃。。对就是这样。头疼…撸码撸的有点多。
总结
- 上一篇: Python 字符串、时间、日期、时间戳
- 下一篇: cannot import name U