文件里排序
#include<stdio.h> #include<process.h>
#include<string.h>
struct staff
{
char name[8];
char num[10];
char sex[10];
char age[10];
char record[10];
char position[15];
char wanges[10];
char tel[15];
char addr[50];
}worker[10]; /*定义一个结构体*/
char filename[20]; /*文件名*/
FILE *fp;
void creat(); /*定义一个创建函数*/
void output(); /*定义一个输出信息的函数*/
void search(); /*定义一个查询的函数*/
void append(); /*定义一个添加函数*/
void modify(); /*定义一个修改函数*/
void delete1(); /*定义一个删除函数*/
void pai();
void main() /*主函数*/
{
int m;
creat();
while(1)
{
printf("\n\n添加,请按1");
printf("\n查找,请按2");
printf("\n修改,请按3");
printf("\n删除,请按4");
printf("\n输出,请按5");
printf("\n排序, 请按6");
printf("\n退出,请按0\n"); /*输出提示信息*/
scanf("%d",&m);
if(m>=0&&m<=6)
{
switch(m)
{
case 1: append();
break;
case 2: search();
break;
case 3: modify();
break;
case 4: delete1();
break;
case 5: output();
break;
case 6: pai();
break;
case 0: exit(0);
}
printf("\n\n操作完毕,请再次选择!");
}
else
printf("\n\n选择错误,请再次选择!");
}
}
void creat()
{
struct staff one;
printf("\n请输入文件名:");
scanf("%s",filename); /*创建一个文件的名字*/
if((fp=fopen(filename,"w"))==NULL) /*打开文件,实现向文件输入数据*/
{
printf("\n不能建立!");
exit(0);
}
fprintf(fp,"%-10s%-8s%-5s%-5s%-10s%-8s%-8s%-10s%-15s\n","姓名","工号","性别","年龄","学历","职位","工资","电话号码","住址");
printf("\n请输入职工信息(以0结束)\n");
printf("姓名:\n");
scanf("%s",one.name);
while(strcmp(one.name,"0"))
{
printf("工号:\n");
scanf("%s",one.num);
printf("性别:\n");
scanf("%s",one.sex);
printf("年龄:\n");
scanf("%s",one.age);
printf("学历:\n");
scanf("%s",one.record);
printf("职位:\n");
scanf("%s",one.position);
printf("工资:\n");
scanf("%s",one.wanges);
printf("电话:\n");
scanf("%s",one.tel);
printf("住址:\n");
scanf("%s",one.addr); /*输出职工的相关信息*/
fprintf(fp,"%-10s%-8s%-5s%-5s%-10s%-8s%-8s%-10s%-15s\n",one.name,one.num,one.sex,one.age,one.record,one.position,one.wanges,one.tel,one.addr);
scanf("%s",one.name);
}
fclose(fp); /*关闭文件*/
}
void output() /*输出函数*/
{
struct staff one;
if((fp=fopen(filename,"r"))==NULL) /*打开文件*/
{
printf("\n不能打开该文件!");
exit(0);
}
printf("\n\n%20s\n","职工信息管理系统");/*向文件输入数据*/
while(!feof(fp))
{
fscanf(fp,"%s%s%s%s%s%s%s%s%s\n",one.name,one.num,one.sex,one.age,one.record,one.position,one.wanges,one.tel,one.addr);
printf("%-10s%-8s%-5s%-5s%-10s%-8s%-8s%-10s%-15s\n",one.name,one.num,one.sex,one.age,one.record,one.position,one.wanges,one.tel,one.addr);
}
fclose(fp);/*关闭文件*/
}
void append() /*添加函数*/
{ struct staff one;
if((fp=fopen(filename,"a"))==NULL) /*打开文件*/
{
printf("\n不能打开该文件!");
exit(0);
}
printf("\n请输入添加职工信息(姓名、工号、性别、年龄、学历、职位、工资、电话、地址)\n");/*向文件中输入数据*/
printf("姓名:");
scanf("%s",one.name);
printf("工号:");
scanf("%s",one.num);
printf("性别:");
scanf("%s",one.sex);
printf("年龄:");
scanf("%s",one.age);
printf("学历:");
scanf("%s",one.record);
printf("职位:");
scanf("%s",one.position);
printf("工资:");
scanf("%s",one.wanges);
printf("电话:");
scanf("%s",one.tel);
printf("地址:");
scanf("%s",one.addr);
fprintf(fp,"%-10s%-8s%-5s%-5s%-10s%-8s%-8s%-10s%-15s\n",one.name,one.num,one.sex,one.age,one.record,one.position,one.wanges,one.tel,one.addr);
fclose(fp); /*关闭文件*/
}