如何用C读取文本文档中的某一行

有一个文本文档,每一行都有姓名,性别,年龄,如果我只是想显示年龄为20岁的所有的信息(包括姓名,性别,年龄)怎么做?要用C来做!谢谢了!
就是以回车为标识来确定是否是一行 ,我肯定还会提高悬赏的,只为求正确答案,谢谢了!

没有能读一行的吧,如果能用C语言实现你的要求,加分的话我给你做。先说明我不会读一行的,因为一行有大小,比如你打开一个文本,当你缩小的时候行数会增多,放大文本的时候行数就减小,这样你怎么能保证每次读出的数据都一样呢?

以下是我对本题的答案:
/*************************
注:要求文件名必须和程序里写的一样,否则打开会失败
输入格式如:name sex age 中间的空格已处理(随便几个)
可直接在文本文件里输入然后运行该程序,或改源代码用程序输入。
**********************/

#include <stdio.h>
#include <string.h>

/***学生信息******/
typedef struct student
{
char *pStrName;
char *pStrSex;
int age;
struct student *next;
}STD;

/***将信息转成学生结构体****/
STD* setStudent(char array[], int len, int age)
{
STD *p = NULL;
char chName[255];
char chSex[50];
char chAge[50];
int tage;
int index;
int tem;

/***分离名字*****/
for (index = 0; index < len && array[index] != ' '; index++)
{
chName[index] = array[index];
}
chName[index] = '\0';
/*****去掉空格*******/
for (; index < len && array[index] == ' '; index++);

/***分离性别*****/
for (tem = 0; index < len && array[index] != ' ';)
{
chSex[tem++] = array[index++];
}
chSex[tem] = '\0';
for (; index < len && array[index] == ' '; index++);

/***分离年龄*****/
for (tem = 0; index < len && array[index] != ' ';)
{
chAge[tem++] = array[index++];
}
chAge[tem] = '\0';
tage = strtol(chAge, NULL, 10);/****字符到数字的转换(库函数)*****/

/*****如果为你选要显示的年龄则生成学生结点并置相应值*****/
if (tage == age)
{
p = (STD*)malloc(sizeof(STD));/****这里是判申请内存是否成功,以下类同*****/
if (p)
{
p->next = NULL;
p->pStrName = (char *)malloc(sizeof(chName) + 1);
if (p->pStrName)
{
strcpy(p->pStrName, chName);
}
else
{
printf("allocate memory(chName) error!\n");
getch();
exit(0);
}
p->pStrSex = (char *)malloc(sizeof(chSex) + 1);
if (p->pStrSex)
{
strcpy(p->pStrSex, chSex);
}
else
{
printf("allocate memory(chSex) error!\n");
getch();
exit(0);
}
p->age = age;
}
else
{
printf("allocate memory(STD) error!\n");
getch();
exit(0);
}
}

return p;
}
/***从文本中读取信息********/
STD* readFile()
{
STD s;
STD *pt = NULL;
STD *ph = &s;

FILE *pf;
char chBuf[255];
int index = 0;
char chTem = 'c';
int age = 20;/*所要查找的学生年龄*/
/* 存入文件
pf = fopen("testFile.txt", "w");
if (pf == NULL)
{
printf("open file error !\n");
}
else
{
fwrite("name sex age\n", 1, sizeof("name\n sex age\n"), pf);
}
fclose(pf);
*/

/*****读取文件*****/
pf = fopen("testFile.txt", "r"); /***默认路径是源文件所在文件****/
if (pf == NULL)
{
printf("open file error !\n");
exit(0);
}
else
{
while (!feof(pf))
{
chTem = fgetc(pf);
chBuf[index++] = chTem;
if (chTem == '\n')
{
chBuf[--index] = '\0';
pt = setStudent(chBuf, index, age);
if (pt)
{
ph->next = pt;
ph = pt;
}
index = 0;
chBuf[index] = '\0';
}
}

fclose(pf);
}

ph = s.next;
return ph;
}

/*****输出选出的学生信息*******/
void displayStu(STD *p)
{
printf("\n*********selected student information***********\n");
printf(" name\tsex\tage\n");
while (p)
{
printf("%s %s %d\n", p->pStrName, p->pStrSex, p->age);
p = p->next;
}
printf("\n---------end of display--------\n");
}

void main()
{
STD *p = NULL;
p = readFile();
displayStu(p);

getch();
}

/*已上通过上机*/
/*更多有关C语言的请到我的百度空间一瞧,互相学习*/
/*最近才开始觉得应该把一些东西记在那里,那里找不到的可以和我说,看看我能不能帮上什么忙*/
/*还有做了个九宫图的程序,可惜还没完成.完成我再放上去*/
/**http://hi.baidu.com/毛求香/ihome/myblog**/
温馨提示:答案为网友推荐,仅供参考
相似回答