C语言:在当前目录下的文件中查找记录并输出到屏幕,咋编?

要注:
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;

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-07-08
格式化读取文本到内存中,再自己写个字符串对比函数(详细查询就要把字符串的每个字符(或者说是bit位)都要对比成功,模糊查询对比关键字就可以了),对比成功就可查询到了。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网