要注:
1)文件stu.dat中存有多条记录,每条内容为姓名(最长15字节)、学号(最长11位)和五门功课成绩(整数0~100)。
2)要求输入姓名或学号查找,找到输出该记录全部信息,否则输出Not find。
代码文本:
#include "stdio.h"
#include <string.h>
struct STU{
char nm[21],id[12];
int s[5];
};
int main(int argc,char *argv[]){
struct STU stu;
char ts[21];
FILE *fp;
if(fp=fopen("stu.dat","r")){
printf("Please enter a name or ID...\n");
scanf("%20s",ts);
printf("\nTo find the result is:\n");
while(fscanf(fp,"%s%s%d%d%d%d%d",stu.nm,stu.id,stu.s,stu.s+1,stu.s+2,stu.s+3,stu.s+4)!=EOF)
if(strcmp(ts,stu.nm)==0 || strcmp(ts,stu.id)==0){
printf(" %s\t%s%4d%4d%4d%4d%4d\n",stu.nm,stu.id,stu.s[0],stu.s[1],stu.s[2],stu.s[3],stu.s[4]);
fclose(fp);
return 0;
}
printf("Not find...\n");
fclose(fp);
}
else
printf("Open the file failure, exit...\n");
return 0;
}