- 1
- 0
- 约4.1千字
- 约 6页
- 2022-11-15 发布于四川
- 举报
〃文件的应用/*文件的应用*/#include stdio.h#include string.h
#include malloc.h#define MaxRec 100 typedef struct Index (
char num[8];
long offset;
}Index;typedef struct sdata(
char num[8];
char name[10];
int sex;
int age;
char addr[30];
char dep[20];
char spec[20J;} Student;〃最多的记录个数〃定义索引文件结构
//学号〃主文件中的记录号
〃定义主文件结构
〃学号
〃姓名
〃性另IJ
//年龄
〃地址
〃系别
〃专业
/*清除主文件和索引文件全部记录*/void DelAll(){FILE *mfile,*idxfile;
if((mfile=fopen(nmain.dat,nwbn))==NULL)(printf( ?不能打开主文件\n”);
return;if ((idxfile=fopen(index.dat” Jwb))二二NULL)
printf( 不能建立索引文件\n”); return;)fclose(mfile);fclose(idxfile); )
/*对R[0..n.l]按递增有序进行直接插入排序*/void InsertSort(Index R[J,int n) ( int i,j;Index temp;for (i=l;in;i++) (
temp=R[i];
while (j=0 strcmp(temp.num,R[j].num)0) (R[j+l]=R|j];//将关键字大于R[i].key的记录后移―;
)
R[j+l]=temp;〃在 j+1 处插入 R[i]/*建立索引文件*/void CreatIdxFile()(
FILE *mfile,*idxfile;Index idx|MaxRec];Student st;int n=O,i;
if((mfile=fopen(,,main.dat;,rb,,))==NULL){
printf(u 不能打开主文件\n)
return;)if((idxfile=fopen(Hindex.datH;,wbH))==NULL)(
printf(u ?不能建立索引文件\n”);
return;)i=0;
while ((fread(st,sizeof(Student), 1 ,mfile))!=NULL) (
strcpy(idx[i].num,st.num);
idx[i].offset=++n;
i++;)/*对idx数组按no域值排序*/InsertSort(idx,n);
rewind(idxfile);for (i=0;in;i++)
fwrite(idx[i],sizeof(Index), 1 ,idxfile);fclose(mfile);fclose(idxfile);)
/*添加一个主文件记录*/void InputMainFile()FILEStudent st;
mfile=fopen(nmain.dat,nab+n);if (mfile==NULL)(
printf( 不能建立主文件\n”);
return;)printf(H 学号,姓名,性别,年龄,地址,系别,专业:”);scanf(,%s%s%d%d%s%s%s,,,st.num,st.name,st.sex,st.age,st.addr,st.dep,st.spec); if (fwrite(st,sizeof(Student),l ,mfile)!=l)
(
printf( 写主文件错误\n);
return;)fclose(mfile);)
void OutputMainFile()〃输出主文件全部记录(
FILE *mfile;Student st;int i=0;mfile=fopen(nmain.dat,rbH);
if (mfile==NULL)(
printf( ?不能读主文件\n);
return;)
while ((fread(st,sizeof(Student), 1 ,mfile))!=NULL)(
printf(H i 己录号 %d:,++i);
printf(%s %s %d %d %s %s %s\nn,st.num,st.name,st.sex,st.age,st.addr,st.dep,st.spec);fclose(mfile);void OutputIdxFile()〃输出索引文件全部记录(
FILE *idxfile;Index irec;int i=0;idx 行 le 二 fopen(index
您可能关注的文档
- 企业文化培训考试.docx
- 仁爱英语九年级unittopic知识点归纳.docx
- 产科皮质醇增多症性多毛患者护理要点详解.docx
- 产科妊娠期呕吐患者护理要点详解.docx
- 交换生入学申请表.docx
- 二次根式的加减研修计划单.docx
- 九年级英语鲁教版五四制--Unit 6 测试.docx
- 中班音乐教案及教学反思《快乐的六一》.docx
- 中班音乐教案《懒惰虫》.docx
- 中小学教育管理,讲义第十二章 学校教学管理.docx
- (正式版)DB51∕T 1867-2014 《袋栽黑木耳生产技术规程》.docx
- (正式版)DB51∕T 2413-2023 《油橄榄密植丰产栽培技术规程》.docx
- (正式版)DB51∕T 2436-2017 《川菜东坡一品肉烹饪工艺技术规范》.docx
- (正式版)DB51∕T 2396-2017 《农村电子商务服务站(点)服务与管理规范》.docx
- (正式版)DB51∕T 2419-2017 《桢楠扦插育苗技术规程》.docx
- CN105145773B 一种无花果曲奇饼干及其制作方法 (江苏农林职业技术学院).docx
- CN105203825A 微测量电极的制作方法和热电势的测量方法及相关装置 (国家纳米科学中心).docx
- CN105137533B 一种啁啾光纤光栅及其制作方法 (南京航空航天大学).docx
- (正式版)DB51∕T 2453-2018 《巴山新居公共管理指南》.docx
- (正式版)DB51∕T 1892-2014 《川西北地区沙化土地治理技术规程》.docx
原创力文档

文档评论(0)