链表和文件!
我不知道这为什么运行不出来结果啊,是想把文件里头的内容读到链表中!烦劳各位大侠帮助小弟,将不胜感激!#include<stdio.h>
#include<stdlib.h>
#define BUFSIZE 10
struct filetext{
char buf[BUFSIZE];
struct filetext *next;
};
struct filetext * readfile(struct filetext * head)
{
struct filetext * p1 = (struct filetext *)malloc(sizeof(struct filetext));
FILE * fp;
struct filetext * p =head;
if((fp=(fopen("filename","rt")))==NULL)
{
printf("open file failure");
getchar();
exit(1);
}
if(fread(p1->buf,BUFSIZE,1,fp)<1)
{
printf("read file failure");
return head;
}
fclose(fp);
if(!head)//如果传进来的head是个空指针,那么新指针就作为头节点返回
{
p1->next = NULL;
return p1;
}
while(p->next) p = p->next;
p->next = p1;
p1->next = NULL;
return head;
}
main()
{
struct filetext *p;
struct filetext *readfile(p);
}
我不知道这为什么运行不出来结果啊,是想把文件里头的内容存储到