c/school/ch10_10-9.c · 1697 字节 · 下载原文
#include <stdio.h>
#include <string.h> //strcpy
// 定义职工结构体
typedef struct employee{
int num,age,salary;
char name[10],addr[50],health[10],class[10];
char sex;
}employee;
employee emp[10];
void del(employee emp[],int k){
for(int i=k-1;i+1<4;i++){
emp[i].num=emp[i+1].num;
emp[i].age=emp[i+1].age;
emp[i].salary=emp[i+1].salary;
emp[i].sex=emp[i+1].sex;
strcpy(emp[i].name,emp[i+1].name);
strcpy(emp[i].addr,emp[i+1].addr);
strcpy(emp[i].health,emp[i+1].health);
strcpy(emp[i].class,emp[i+1].class);
}
}
int main(){
char file_path_r[100]="/home/mz/我的/书籍/C语言/谭老C程序设计/第十章/10-9/employee.txt";
char file_path_w[100]="/home/mz/我的/书籍/C语言/谭老C程序设计/第十章/10-9/salary.txt";
FILE* fp1=fopen(file_path_r,"r+");
FILE* fp2=fopen(file_path_w,"w+");
int n;
for(int i=0;i<4;i++){
fscanf(fp1,"%d %s %c %d %s %d %s %s",&emp[i].num,emp[i].name,&emp[i].sex,&emp[i].age,emp[i].addr,&emp[i].salary,emp[i].health,emp[i].class);
fprintf(fp2,"%4s %3d\n",emp[i].name,emp[i].salary);
}
fclose(fp1);
fclose(fp2);
fp1=fopen(file_path_r,"w+");
puts("输入要删除位置(输入0结束)");
while(scanf("%d",&n),n!=0){
// 删除第n个职工
del(emp,n);
// 将删除后数据放回原文件
for(int i=0;i<3;i++){
fprintf(fp1,"%d %s %c %d %s %d %s %s\n",emp[i].num,emp[i].name,emp[i].sex,emp[i].age,emp[i].addr,emp[i].salary,emp[i].health,emp[i].class);
}
puts("输入要删除位置(输入0结束)");
}
fclose(fp1);
return 0;
}