本篇文章给大家谈谈c语言链表读文件,以及c语言链表从文件读取对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、c语言,从文件中读取单链表并输出。2、C语言链表读取二进制文件3、c语言如何从文件读入,并存放在链表中4、C语言怎么把文件的内容读到链表里面?5、C语言链表的读取
c语言,从文件中读取单链表并输出。
需求有点不清晰,你要从文件里取什么东西出来?
我改了从txt取每一行的字符串出来,记录在你的链表,你参考一下
#include
“stdafx.h”
#include
“stdlib.h”
int
main()
{
struct
fac
{
//int
data;
char
data[256];
//不知道你要取什么数据,这里用个字符串数组代替
struct
fac
*next;
}*phead;
int
i;
FILE
*fp=fopen(“d:\\text.txt”,”rb”);
//一个有内容的txt文本,自己替换
struct
fac
*p;
struct
fac
*ptemp;
phead=(struct
fac*)malloc(sizeof(struct
fac));
phead-next=NULL;
ptemp=phead;
//fread(p,sizeof(struct
fac),1,fp);
while(fgets(
ptemp-data,256,fp
)!=NULL)//改用fgets取一行的数据
{
printf(“%s\n”,ptemp-data);
p=(struct
fac*)malloc(sizeof(struct
fac));
ptemp-next=p;
ptemp
=
ptemp-next;
}
//后面还应该有个释放链表的操作,这里程序结束会回收,就不写了。
}
C语言链表读取二进制文件
关键代码应该是这样的
FILE *fp = fopen(“file”,”rb”);
char *p = node-data;
while(!feof(fp))
*p++ = fgetc(fp);
fclose(fp);
//**********************以上读*******************
FILE *fp = fopen(“file”,”wb”);
char *p = node-data;
while( p node-data+len)
fputc(fp,*p++);
fclose(fp);
//**********************以上写*******************
c语言如何从文件读入,并存放在链表中
//举个单链表的例子,首先定义链表成员的结构体
struct filetext{char buf[BUFSIZE];
struct filetext *next;};
//读取文件,并插入进链表的函数,filename为要读取的文件名,head为链表的头节点,函数返回插入新节点后链表的头节点
struct filetext * readfile(char * filename,struct filetext * head)
{ struct filetext * new = (struct filetext *)malloc(sizeof(struct filetext));//定义一个新成员,并给它分配空间
FILE * fp;//读取文件的文件流
struct filetext * p =head;//定义一个p,用来寻找链表中最后一个节点
if((fp=(fopen(filename,”r+”)))==NULL)
{//如果打开文件失败,返回head,并提示
printf(“open file failure”);
return head; }
//然后开始读取文件,放到new的buf中
if(fread(new-buf,BUFSIZE,1,fp)1)
{ //如果读取失败,提示,并返回head
printf(“read file failure”);
return head; }
fclose(fp);
//文件读取完后,进行链表操作
if(!head)//如果传进来的head是个空指针,那么新指针就作为头节点返回
{ new-next = NULL;
return new; }
while(p-next) p = p-next;//把p移动到最后一个节点
p-next = new;//p的下一个节点为new
new-next = NULL;//new的下一个节点为空
return head;
//这样这个函数就完成了,你可以写个主函数,定义一个头节点,试下。
扩展资料:
线性表的链式存储表示的特点是用一组任意的存储单元存储线性表的数据元素(这组存储单元可以是连续的,也可以是不连续的)。
因此,为了表示每个数据元素 与其直接后继数据元素 之间的逻辑关系,对数据元素 来说,除了存储其本身的信息之外,还需存储一个指示其直接后继的信息(即直接后继的存储位置)。由这两部分信息组成一个”结点”(如概述旁的图所示),表示线性表中一个数据元素。
线性表的链式存储表示,有一个缺点就是要找一个数,必须要从头开始找起,十分麻烦。根据情况,也可以自己设计链表的其它扩展。但是一般不会在边上附加数据,因为链表的点和边基本上是一一对应的(除了第一个或者最后一个节点,但是也不会产生特殊情况)。
不过有一个特例是如果链表支持在链表的一段中把前和后指针反向,反向标记加在边上可能会更方便。
参考资料:链表_百度百科
C语言怎么把文件的内容读到链表里面?
struct Bookinfo\x0d\x0a{\x0d\x0achar num[20]; //书号\x0d\x0a char name[10]; //书名\x0d\x0a int jinjia; //进价\x0d\x0a int shoujia; //售价\x0d\x0a int shuliang; //库存数量\x0d\x0a int shouchu; //售出\x0d\x0a};\x0d\x0atypedef struct Node_book* pNode_book;\x0d\x0astruct Node_book\x0d\x0a{\x0d\x0astruct Bookinfo bookinfo;\x0d\x0apNode_book next;\x0d\x0a};\x0d\x0a只存节点的数据域,以二进制文件存放:\x0d\x0aint save(struct pNode_book head)\x0d\x0a{\x0d\x0aif(!head) return 0;\x0d\x0aFILE *fp=fopen(“info.data”,”wb”);\x0d\x0aint i=0;\x0d\x0awhile(head)\x0d\x0a{\x0d\x0afwrite(head-bookinfo,sizeof(Bookinfo),1,fp);\x0d\x0ai++;\x0d\x0ahead=head-next;\x0d\x0a}\x0d\x0afclose(fp);\x0d\x0a\x0d\x0areturn i;\x0d\x0a} \x0d\x0a\x0d\x0aint readFromFile(struct pNode_book *head)\x0d\x0a{\x0d\x0aFILE *fp=fopen(“info.data”,”rb”);\x0d\x0aif(!fp)\x0d\x0a{\x0d\x0aprintf(“Can not open the file!\n”);\x0d\x0areturn 0;\x0d\x0a}\x0d\x0a\x0d\x0astruct pNode_book pCur=NULL;\x0d\x0afseek(fp,0,SEEK_END);\x0d\x0along end=ftell(fp);\x0d\x0afseek(fp,0,SEEK_SET);\x0d\x0aint i=0;\x0d\x0aif(ftell(fp)!=end)\x0d\x0a{\x0d\x0apNode_book tmpNode=(pNode_book)malloc(sizeof(Node_book));\x0d\x0atmpNode-next=NULL;\x0d\x0a\x0d\x0afread(tmpNode-bookinfo,sizeof(Bookinfo),1,fp);\x0d\x0ai++;\x0d\x0a\x0d\x0a*head=tmpNode;\x0d\x0apCur=*head;\x0d\x0a}\x0d\x0aelse\x0d\x0a{\x0d\x0aprintf(“No record!\n”);\x0d\x0areturn 0;\x0d\x0a}\x0d\x0a\x0d\x0awhile(ftell(fp)!=end)\x0d\x0a{\x0d\x0apNode_book tmpNode=(pNode_book)malloc(sizeof(Node_book));\x0d\x0atmpNode-next=NULL;\x0d\x0a\x0d\x0afread(tmpNode-bookinfo,sizeof(Bookinfo),1,fp);\x0d\x0ai++;\x0d\x0a\x0d\x0apCur-next=tmpNode;\x0d\x0apCur=pCur-next;\x0d\x0a}\x0d\x0a\x0d\x0afclose(fp);\x0d\x0areturn i;\x0d\x0a} \x0d\x0a\x0d\x0a//在vc++下编译。如果在TC下,可能还要做些小修改。\x0d\x0a//我在记事本上写的,你调试下吧!\x0d\x0a//有问题Hi我!
C语言链表的读取
从void insert(struct student *head,struct student *newNode)和struct student *read()来看,链表是有头节点的,但函数void save(struct student *head)是当作无头结点处理的,或者说,save()函数将头节点的数据(随机产生的)保存到数据文件中了。修改方法很简单,只需将save()的p=head;改为p=head-next;即可。
关于c语言链表读文件和c语言链表从文件读取的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。