用c++读取txt文档并将内容存放到链表里,并且以逗号句号划分按行输出,输入第几行就相应跳出那句话 10
1个回答
展开全部
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;
}
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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询