源代码知识讲解Word文档下载推荐.docx
《源代码知识讲解Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《源代码知识讲解Word文档下载推荐.docx(11页珍藏版)》请在冰点文库上搜索。
//为节点分配内存
printf("
\n请输入文件名:
\n"
);
scanf("
%s"
filename);
if((fp=fopen(filename,"
w+"
))==NULL)
{
\n你没有输入文件名,不能找到文件夹"
exit(0);
}
\n请输入姓名,学号(相同的长度)和房号,以空格隔开,以#结束\n"
person->
name);
while(strcmp(person->
name,"
#"
))//该循环用于控制学生信息的录入当输入#时学生信息录入完毕
%s%s"
xh,person->
fh);
fprintf(fp,"
%-10s%-20s%-50s\n"
name,person->
fclose(fp);
}
voidreadfile()//文件读取函数用于打开已有的二进制数据文件
{printf("
\npleaseenterthefileroad:
//此处输入为文件的路径如c:
\zhang
if((fp=fopen(filename,"
r+"
))==NULL)
\ncan'
topenthefile:
voidoutput()//输出函数用于输出文件的全部信息
longoffset1,offset2;
charname1[8],name2[8],name3[8];
charxh1[16],xh2[16],xh3[16];
charfh1[30],fh2[30],fh3[30];
r"
topenthefile"
while(!
feof(fp))//此循环用于对文件数据中关键字学号进行从小到大冒泡排序
inta,b,c,d;
//a为循环结束判定变量b,c,d用于起泡排序时学号交换的替换
{
}
feof(fp))//从文件头开始遍历
{
feof(fp))//对两组数据进行排序
{
offset1=ftell(fp);
//获取文件内部当前指针位置
fscanf(fp,"
%s%s%s\n"
strcpy(name1,person->
strcpy(xh1,person->
xh);
strcpy(fh1,person->
if(feof(fp))break;
//文件结束跳出循环
offset2=ftell(fp);
//获取文件内部下一指针位置
strcpy(name2,person->
strcpy(xh2,person->
strcpy(fh2,person->
b=strlen(xh1);
c=strlen(xh2);
d=strcmp(xh1,xh2);
if(b==c&
&
d>
0)//如果学号1大于等于学号2则交换全部数据
strcpy(name3,name1);
strcpy(name1,name2);
strcpy(name2,name3);
strcpy(xh3,xh1);
strcpy(xh1,xh2);
strcpy(xh2,xh3);
strcpy(fh3,fh1);
strcpy(fh1,fh2);
strcpy(fh2,fh3);
fseek(fp,offset1,SEEK_SET);
//将文件指针移动offset1个字节
strcpy(person->
name,name1);
strcpy(person->
xh,xh1);
fh,fh1);
fprintf(fp,"
//排序后写入文件
name,name2);
xh,xh2);
strcpy(person->
fh,fh2);
}//if
fseek(fp,offset2,SEEK_SET);
///将文件位置指针从文件头向前移动offset2个字节
}//while对两组数据进行排序
rewind(fp);
//指向头文件
feof(fp))//此循环用于判断学号是否有序
strcpy(xh1,person->
if(feof(fp)){a=1;
break;
d<
=0)a=1;
//若a为1则有序
else
{a=0;
//a为0则无序
}
}//while判断学号是否有序
if(a)break;
//a为1说明学号已经有序跳出循环
}//while从文件头开始遍历
////若a为1,则学号已有序排列,跳出总循环,输出文件
\n******************************************************************\n"
%35s\n"
"
thefile(thefilehavaprintfasstudentnumber)"
name"
studentnumber"
roomnumber"
feof(fp))//次循环用于输出文件
*******************************************************************\n\n"
voidsearch1()//按姓名搜索函数
intk=0;
charnamekey[8];
\npleaseenterthenameheadyouneed:
"
namekey);
rb"
topenthefile"
feof(fp))
%s%s%s\n"
if(!
strcmp(namekey,person->
name))//比较是否相同相同则输出结果否则输出未找到记录
\n\nhaveserched,thecourrentis:
k=1;
k)printf("
\n\nsorry,thereisnotthecurrentofthisspeople\n"
voidsearch2()//按学号搜索函数
charxhkey[16];
\npleaseentertheroomnumberyouwanttoserch:
xhkey);
topenthefile"
strcmp(xhkey,person->
xh))////比较是否相同相同则输出结果否则输出未找到记录
\n\nhavegotit,thecurrentis:
voidsearch3()//按房号搜索函数
charfhkey[30];
\npleaseentertheroomnumberyouwanttoserch:
fhkey);
strcmp(fhkey,person->
fh))//比较是否相同相同则输出结果否则输出未找到记录
\n\nhavegotit,thecurrentis:
\n\nsorry,thereisnotthecurrentofthisspeople\n"
voidmain()//主函数
intm,flag=1;
//m用于控制菜单的选择项flag用于控制菜单弹出
while(flag)
%33s\n"
thefile"
*****************************************************************\n"
\t\t0-------------创建一个新的学生数据文件\n"
\t\t1-------------读取一个已经建立的文件\n"
\t\t2------------按名字进行查询\n"
\t\t3------------按学号进行查询\n"
\t\t4------------按宿舍号进行查询\n"
\t\t5-------------输出按学号进行排序的文件\n"
\t\t6-------------退出\n"
\t请选择(0-----------6)\n"
%d"
&
m);
switch(m)
{case0:
creat();
case1:
readfile();
case2:
search1();
case3:
search2();
case4:
search3();
case5:
output();
case6:
exit(0);