当前位置:首页 » 编程语言 » c语言文件链表

c语言文件链表

发布时间: 2022-08-10 12:25:45

1. c语言,关于保存链表到文件和从文件装载链表(高手请进,急)

拿去用吧~
#include<stdio.h>
#include<malloc.h>
#include<string.h>
structaddress
{
inta;
intb;
charc;
address*next;
};
voidSaveToFile(structaddress*p,FILE*fp)
{
if(p!=NULL)
{
do
{
fwrite(p,sizeof(structaddress),1,fp);
p=p->next;
}
while(p!=NULL);
}
}
intload(FILE*fp,structaddress**head,int&n)//读取文件数据,建立链表
{
structaddress*p1,*p2;
*head=(structaddress*)malloc(sizeof(structaddress));
memset(*head,0,sizeof(structaddress));
if(fread(*head,sizeof(structaddress),1,fp)!=1)
{
free(*head);
*head=NULL;
return(0);
}
p2=*head;
n++;
while(!feof(fp))
{
p1=(structaddress*)malloc(sizeof(structaddress));
fread(p1,sizeof(structaddress),1,fp);
p2->next=p1;
p2=p1;
n++;
}
p2->next=NULL;
return(n);
}
voidmain()
{
structaddress*head;
intn=0;
structaddress*Test[10]={0};
inti=0;
FILE*fp=NULL;
for(;i<10;i++)
{
Test[i]=(structaddress*)malloc(sizeof(structaddress));
if(Test[i]!=NULL)
{
memset(Test[i],0,sizeof(structaddress));
//输入你的数据(a,b,c)
//如下
Test[i]->a=100+i;//不用则注释掉
}
}
for(i=0;i<10;i++)
{
if(i<10)
{
Test[i]->next=Test[i+1];//将结构指针连成链表
}
}
if((fp=fopen("addrbook","wb"))!=NULL)
{
SaveToFile(Test[0],fp);
fclose(fp);
}
if((fp=fopen("addrbook","rb"))!=NULL)
{
load(fp,&head,n);
}
}

2. C语言怎么从文件中将信息导入链表中

这个实现起来挺简单的,就是写起来麻烦点,不是一点代码能写完的!
1,建立一个链表,链表的节点struct定义为联系人信息的格式;
2,读取文件,把内容存入链表;
3,查询就根据姓名关键字遍历链表就行了;
4,把内容存入文件;

首先建立链表,以及插入节点,查询链表函数写出来;
文件的读取和存入到不是很麻烦;
----------------------------下面是简单的实现,可以输入,存入文件,从文件读取,打印,如果还想要实现其他的稍修改一下就行了------

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

#define MAX 80

struct stu{
int id;
char name[MAX];
struct stu* next;
};

typedef struct stu STU;
typedef STU* STUPTR;

STUPTR insert(STUPTR head, int id, char* name);
void print_st(STUPTR head);
void save_to_file(FILE* fp, STUPTR head);
FILE* crt_file( void );
STUPTR read_to_link(STUPTR head);

int main( void )
{

int choice;
int stu_id;
char stu_name[MAX];

STUPTR head;

FILE* fp;

clrscr();
head = NULL;

clrscr();

printf( "please enter the choice!\n" );
scanf( "%d", &choice );

while( choice != 9 ){

switch(choice){
case 1: printf("enter the insert ----id---name!\n");
scanf( "%d%s", &stu_id ,stu_name);
head = insert(head,stu_id,stu_name);
break;

case 2:print_st(head);
break;

case 3:puts("save the info to file e:\stu_info.txt!\n");
fp = crt_file();
save_to_file( fp, head);
puts( "save the data sucessful!\n");
fclose(fp);
break;

case 4:puts("read the file to link!\n");
head = NULL;
head = read_to_link(head);
puts("read the file successful!\n");
break;

}

printf( "please enter the choice!\n");
scanf( "%d", &choice );
}

}

STUPTR insert(STUPTR head, int id, char* name)
{
STUPTR new, cur;

cur = head;

new = malloc( sizeof( STU) );

new->id = id;
strcpy(new->name, name);
new->next = NULL;

if(cur == NULL)
head = new;
else{
while(cur->next != NULL){
cur = cur->next;
}
cur->next = new;
}

return head;
}

void print_st(STUPTR head)
{
STUPTR cur=head;
int i;
i=1;

if(cur == NULL){
printf( "has no student info!\n" );
}
else while(cur != NULL){
printf( "%d:------%d---%s\n", i,cur->id,cur->name);
i++;
cur = cur->next;

}

}

void save_to_file(FILE* fp, STUPTR head)
{
int i;
STUPTR cur;

cur = head;

while(cur != NULL){
fprintf(fp, "%d %s\n", cur->id,cur->name);
cur = cur->next;
}
}

FILE* crt_file(void)
{
FILE* fp;
fp = fopen( "e:\stu_info.txt", "w" ); /*w is right or not*/
if(fp == NULL)
puts("shit!!!!!!!!!!");
return fp;
}

STUPTR read_to_link( STUPTR head)
{

int id;
char name[MAX];

FILE* fp;
fp = fopen("e:\stu_info.txt", "r");

while( !feof(fp) ){
fscanf(fp, "%d%s", &id, name );
head = insert(head, id, name);
}

return head;

}

3. 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;

//这样这个函数就完成了,你可以写个主函数,定义一个头节点,试下。

(3)c语言文件链表扩展阅读:

线性表的链式存储表示的特点是用一组任意的存储单元存储线性表的数据元素(这组存储单元可以是连续的,也可以是不连续的)。

因此,为了表示每个数据元素与其直接后继数据元素 之间的逻辑关系,对数据元素 来说,除了存储其本身的信息之外,还需存储一个指示其直接后继的信息(即直接后继的存储位置)。由这两部分信息组成一个"结点"(如概述旁的图所示),表示线性表中一个数据元素。

线性表的链式存储表示,有一个缺点就是要找一个数,必须要从头开始找起,十分麻烦。根据情况,也可以自己设计链表的其它扩展。但是一般不会在边上附加数据,因为链表的点和边基本上是一一对应的(除了第一个或者最后一个节点,但是也不会产生特殊情况)。

不过有一个特例是如果链表支持在链表的一段中把前和后指针反向,反向标记加在边上可能会更方便。

4. C语言链表与文件

问题一:这个原来的dat文件需要是已经在之前通过C语言形成的链表数据吗?
答:不一定是链表数据,也可能是数组什么的。
问题二:是的话,如何提取出来,提取出来后就已经是链表?不是的话,应该怎么构造?
答:提取出来的话就是,怎么写入就怎么读出,写入方式和读出方式一样才行。至于是用链表保存还是数组这是随便的!
问题三:最后形成的dat文件是不是以链表形式保存?
答案同上~!

5. 关于c语言把文件读入链表

把文件读入程序与程序读入链表当两回事来做,
**首先先定义一个节点形式
struct node {
char [20] date;
char [20] time;
char [20] place;
char [20] person;
char[20] event;
struct node* next;
};

**1.把文件输入程序
//先定义一个struct 结构体临时存储文件
struct node *p1 = (struct node *) malloc (sizeof( struct node ));
//然后存入数据
fscanf(events,"%s,%s,%s,%s,%s",p1->date,p1->time,p1->place,p1->person,p1->event);

**2.把这个struct弄到链表当中区
自己可以建一个函数
void struct_connect_linkList( struct node head, struct node *p){
//这边往前面插入的链表;
p->next = head->next;
head = p;
}
然后调用函数就行了 struct_connect_linkList(head, p1); //head就是链表的头

//这里只是其中一种思路,仅供参考

6. 关于C语言文件数据写入链表的问题

#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
typedef struct Node
{
int num;
int score;
struct Node* next;
}Node, *Linklist;

void InitLinklist(Linklist* L) //初始化单链表,建立空的带头结点的链表
{
*L = (Node*)malloc(sizeof(Node));
(*L)->next = NULL;
}

void CreateLinklist(Linklist L) //尾插法建立单链表
{
Node *r, *s;
r = L;
int iNum, iScore;
printf("Please input the number and score:\n");
scanf("%d",&iNum);
scanf("%d",&iScore);
while(0 != iScore) //当成绩为负时,结束输入
{
s = (Node*)malloc(sizeof(Node));
s->num = iNum;
s->score = iScore;
r->next = s;
r =s;
printf("Please input the number and score:\n");
scanf("%d",&iNum);
scanf("%d",&iScore);
}
r->next = NULL; //将最后一个节点的指针域置为空
}

int WriteLinklistToFile(const char* strFile, Linklist L)
{
FILE *fpFile;
Node *head = L->next;
if(NULL == (fpFile = fopen(strFile,"w"))) //以写的方式打开
{
printf("Open file failed\n");
return FALSE;
}

while(NULL != head)
{
fprintf(fpFile,"%d\t%d\n",head->num,head->score);
head = head->next;
}

if(NULL != fpFile)
fclose(fpFile);
return TRUE;
};

int ReadFromFile(const char* strFile)
{
FILE *fpFile;
if(NULL == (fpFile = fopen(strFile,"r"))) //以读的方式打开
{
printf("Open file failed\n");
return FALSE;
}

printf("The contents of File are:\n");
while(!feof(fpFile))
putchar(fgetc(fpFile));//证明fprintf()输出到文件中的绝对是字符串

if(NULL != fpFile)
fclose(fpFile);
return TRUE;
}

void Destroy(Linklist L)
{
Node *head =L;
while (head)
{
Node* temp = head;
head = head->next;
free(temp);
}
}

int main()
{
char* strName = "test.txt";
Linklist L;
InitLinklist(&L);
CreateLinklist(L);
WriteLinklistToFile(strName, L);
ReadFromFile(strName);
Destroy(L);
return 0;
}

7. c语言中怎么根据文件建立一个链表

依次是编号
名字
数据是么?你需要先建立一个creat.txt文件,然后对文件进行操作~!
头文件用#include<stdio.h>
#include<stdlib.h>这两个,然后定义个文件指针
FILE
*fp;
用fopen("creat.txt","r")进行写入,写入用fscanf(fp,"%f%s%f",&id,&name,&grade);操作完成关闭文件fclose(fp);这样就行了~!在链表里插入这些东西就可以进行写入了。。具体还是靠自己写,要考试了,具体程序没有时间给你写。。

8. C语言怎么把文件的内容读到链表里面

当把链表已经确定的时候,就可以依次存入文件。

和平时链表的遍历一样,每读取一个节点内容就进行一次存入操作。

不过要注意几个部分的检查:

  1. 内存空间是否分配成功

  2. 是否成功存入到文件中

  3. 在工作完成之后,是否将以后不会用到的变量清空和删除。


按照问题要求的代码如下:

Consumer*read_list()

{

FILE*fp;

if((fp=fopen("CONSUMER.dat","rb"))==NULL)

{

printf("无法读取CONSUMER.dat ");

returnNULL;

}

intsign;

Consumer*s,*p,*head;


head=(Consumer*)malloc(SIZE_C);

if(head==NULL)

{

printf("读取失败!内存空间申请不足! ");

returnNULL;

}

fseek(fp,0,SEEK_END);

if(ftell(fp)==0)

{

returnNULL;

}

p=head;

p->next=NULL;

while(feof(fp))

{

s=(Consumer*)malloc(SIZE_C);

//fread(s,SIZE_C,1,fp);

fread(s,sizeof(char),SIZE_C,fp);

p->next=s;

p=s;

p->next=NULL;

}

fclose(fp);

returnhead;

}//读取文件到链表

intsave_consumer(Consumer*p)

{

FILE*fp;

Consumer*head;

head=p;//p为已经构建好的链表

//if((fp=fopen("CONSUMER.dat","ab+"))==NULL)

if((fp=fopen("CONSUMER.dat","wb"))==NULL)

{

printf("无法打开CONSUMER.dat! ");

return-1;

}

while(p!=NULL)

{

//fwrite(p,SIZE_C,1,fp);

fwrite(p,sizeof(char),SIZE_C,fp);

p=p->next;

}

fclose(fp);

return1;

}//储存链表到文件

热点内容
c语言打印正方形 发布:2024-05-05 16:09:20 浏览:643
编程用箭头 发布:2024-05-05 15:54:21 浏览:794
步骤条源码 发布:2024-05-05 15:35:55 浏览:846
安卓拍光遇视频如何高清 发布:2024-05-05 15:23:20 浏览:934
linuxo文件 发布:2024-05-05 15:19:12 浏览:945
手机服务器地址或者域名 发布:2024-05-05 15:19:09 浏览:374
我的世界服务器版如何登录 发布:2024-05-05 15:17:28 浏览:795
綦江dns服务器地址 发布:2024-05-05 15:04:11 浏览:557
山东省日照市监控服务器地址 发布:2024-05-05 15:03:59 浏览:343
java提升教程 发布:2024-05-05 15:00:51 浏览:145