怎么用C++从一个txt里面读取数据然后存放到链表中进行各种操作 最后读到另外一个txt中?

typedef struct informationTable
{
char name[20];
char sex;
char birth[10];

informationTable *next;
}INFO; 结构体

LiLei M 1988-02-01
Kate F 1986-09-06
John M 1994-12-26
Lucy F 1987-05-17

分数到时候会给你的

C语言标准库是这样做的,参考吧:

typedef struct informationTable
{
char name[20];
char sex;
char birth[10];

struct informationTable *next;
}INFO ,*pINFO; // 结构体

int main(int argc, char *argv[])
{
FILE *pf;
char scrname[20] = {0}, desname[20] = {0};
pINFO head, tail, tmp;

printf("Enter Input Filename:");
gets(scrname);
printf("Enter Output Filename:");
gets(desname);
if(NULL == (pf = fopen(scrname ,"r"))) return 0;
if(NULL == (tmp = (pINFO)malloc(sizeof(*tmp)))) return 0;
while(EOF != fscanf(pf,"%s %c %s", &tmp->name, &tmp->sex, &tmp->birth))
{
if(!head)
{
head = tail = tmp;
tail->next = NULL;
}
else
{
tmp->next = NULL;
tail->next = tmp;
tail = tail->next;
}
if(NULL == (tmp = (pINFO)malloc(sizeof(*tmp)))) break;
}
fclose(pf);
if(tail != tmp)
free(tmp);
/*
head为链表头,可以执行数据操作
*/
if(NULL == (pf = fopen(desname ,"w+"))) return 0;
tail = head;
while(tail)
{
fprintf(pf, "%s %c %s\n", tail->name, tail->sex, tail->birth);
tail = tail->next;
}
fclose(pf);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-01-10
额外的加上这几句就行了

#include "fstream"
ifstream fin("xx.txt");
ofstream fout("xxx.txt");
第2个回答  2012-01-10
你看看fstream的相关资料就会了。
相似回答