⑴ 鏈表文件讀取與寫入的c語言程序代碼怎麼寫啊
一邊讀取鏈表裡面的內容,一邊使用write函數寫進文件;
一邊用read函數讀取文件信息,一邊保存在鏈表中。
鏈表保存信息,必須要結構體,因為要有地址保存下個鏈表的地址
struct
test
{
char
arry[100];//用於保存信息
struct
test
*
next;//用於保存下個內容信息的指針地址
};
這樣就可以把信息鏈接在一起了。
⑵ C語言鏈表的讀取
把你的主函數和操作步驟貼出來唄
⑶ c語言如何將文件數據讀入鏈表用fread
gsl-1.16\cblas.libs 目錄中可以找到如下文件:
libgslcblas.a
libgslcblas.dll.a
libgslcblas-0.dll
⑷ 關於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就是鏈表的頭
//這里只是其中一種思路,僅供參考
⑸ 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;
//這樣這個函數就完成了,你可以寫個主函數,定義一個頭節點,試下。
(5)c語言讀取輸入的鏈表擴展閱讀:
線性表的鏈式存儲表示的特點是用一組任意的存儲單元存儲線性表的數據元素(這組存儲單元可以是連續的,也可以是不連續的)。
因此,為了表示每個數據元素與其直接後繼數據元素 之間的邏輯關系,對數據元素 來說,除了存儲其本身的信息之外,還需存儲一個指示其直接後繼的信息(即直接後繼的存儲位置)。由這兩部分信息組成一個"結點"(如概述旁的圖所示),表示線性表中一個數據元素。
線性表的鏈式存儲表示,有一個缺點就是要找一個數,必須要從頭開始找起,十分麻煩。根據情況,也可以自己設計鏈表的其它擴展。但是一般不會在邊上附加數據,因為鏈表的點和邊基本上是一一對應的(除了第一個或者最後一個節點,但是也不會產生特殊情況)。
不過有一個特例是如果鏈表支持在鏈表的一段中把前和後指針反向,反向標記加在邊上可能會更方便。
⑹ c語言鏈表文件讀取問題
p0指向鏈表中最後一個節點,讀到文件結尾時,feof返回1,於是執行if語句,此時free(p0),豈不是把鏈表最後一個節點刪掉了,導致鏈表最後不指向NULL,被free的節點地址已經不能訪問,但你卻訪問了,所以程序會出錯。
⑺ C語言中如何將文件中的數據讀取到鏈表中
struct Bookinfo
{
char num[20]; //書號
char name[10]; //書名
int jinjia; //進價
int shoujia; //售價
int shuliang; //庫存數量
int shouchu; //售出
};
typedef struct Node_book* pNode_book;
struct Node_book
{
struct Bookinfo bookinfo;
pNode_book next;
};
只存節點的數據域,以二進制文件存放:
int save(struct pNode_book head)
{
if(!head) return 0;
FILE *fp=fopen("info.data","wb");
int i=0;
while(head)
{
fwrite(&head->bookinfo,sizeof(Bookinfo),1,fp);
i++;
head=head->next;
}
fclose(fp);
return i;
}
int readFromFile(struct pNode_book *head)
{
FILE *fp=fopen("info.data","rb");
if(!fp)
{
printf("Can not open the file!\n");
return 0;
}
struct pNode_book pCur=NULL;
fseek(fp,0,SEEK_END);
long end=ftell(fp);
fseek(fp,0,SEEK_SET);
int i=0;
if(ftell(fp)!=end)
{
pNode_book tmpNode=(pNode_book)malloc(sizeof(Node_book));
tmpNode->next=NULL;
fread(&tmpNode->bookinfo,sizeof(Bookinfo),1,fp);
i++;
*head=tmpNode;
pCur=*head;
}
else
{
printf("No record!\n");
return 0;
}
while(ftell(fp)!=end)
{
pNode_book tmpNode=(pNode_book)malloc(sizeof(Node_book));
tmpNode->next=NULL;
fread(&tmpNode->bookinfo,sizeof(Bookinfo),1,fp);
i++;
pCur->next=tmpNode;
pCur=pCur->next;
}
fclose(fp);
return i;
}
//在vc++下編譯。如果在TC下,可能還要做些小修改。
//我在記事本上寫的,你調試下吧!
//有問題Hi我!
⑻ 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語言如何將文件的數據讀入一個鏈表中
#include<stdio.h>
#include<stdlib.h>
structdate
{
charstr[3];
structdate*next;
};
//鏈表長度為len
structdate*create_link(intlen)
{
structdate*head;
structdate*tmp;
inti;
head=malloc(sizeof(structdate));
tmp=head;
for(i=1;i<len;++i)
{
head->next=malloc(sizeof(structdate));
head=head->next;
}
head->next=NULL;
returntmp;
}
//讀文件到鏈表
voidread_file_to_link(structdate*head,FILE*fp)
{
if(head==NULL||fp==NULL)
{
fprintf(stderr,"nullpointer");
exit(EXIT_FAILURE);
}
do
{
fscanf(fp,"%3s",head->str);
head=head->next;
}while(head!=NULL);
}
//顯示鏈表中的內容
voidprint_link(structdate*head)
{
if(head==NULL)
{
fprintf(stderr,"nullpointer");
exit(EXIT_FAILURE);
}
do
{
printf("%s",head->str);
head=head->next;
}while(head!=NULL);
}
intmain()
{
FILE*fp;
intlen;//鏈表長度
scanf("%d",&len);
fp=fopen("a.txt","r");
structdate*head;
head=create_link(len);
read_file_to_link(head,fp);
print_link(head);
exit(EXIT_SUCCESS);
}