C语言中如何从txt文档按姓名查找并显示

如题所述

第1个回答  2010-07-07
#include <stdio.h>
#include <string.h>
int main(void)
{
FILE * f = 0; int i = 0;
char s[256] = "", t[256] ="";
printf(".txt?");
scanf("%s",s);
f = fopen(s, "r");
if(!f) {
printf("bad file %s!\n", s);
return 0;
}
printf("who?");
scanf("%s",s);
while(!feof(f)) {
memset(t, 0, 256);
fgets(t, 255, f);
++i;
if(strstr(t, s)) printf("line %d#, %s", i, t);
}
fclose(f);
return 0;
}本回答被网友采纳
相似回答