--------------c语言 读入txt文件------------------

来源:百度知道 编辑:UC知道 时间:2024/05/16 16:05:26
c语言 读入文件 用链表的数组保存

读入一个txt文件
内容类似:
12sad
12324
346
345

每行一排可能有n排。

如何读取存入一个链表数组里面,
希望高手能提供全代码

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

typedef struct Lnode{
char *data;
struct Lnode *next;
}Lnode, *LinkList;

void InitList(LinkList &L, char buff[50][20], int sc)
{
L=(Lnode *)malloc(sizeof(Lnode));
L->next=NULL;
LinkList q=L;
for(int i=0;i<sc;i++)
{
LinkList p=(Lnode *)malloc(sizeof(Lnode));
q->next=p;
p->next=NULL;
p->data=(char *)malloc(sizeof(char)*20);
strcpy(p->data,buff[i]);
q=q->next;
}

}//初始化链表L

void TraverseList(LinkList L)
{
LinkList p;
p=L->next;
while(p)
{
printf("%s ",p->data);
p=p->next;
}
printf("\n");
}//遍历链表L

void main()
{
FILE *fi;
LinkList L;

int elemnum = 0, k = 0;
char buff[50][20];

if ((fi=fopen(&quo