當前位置:首頁 » 編程語言 » 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;

}//儲存鏈表到文件

熱點內容
怎麼查看我的wifi密碼 發布:2024-04-25 18:54:43 瀏覽:756
fckeditorforjava 發布:2024-04-25 18:50:27 瀏覽:623
優酷上傳視頻需要多久 發布:2024-04-25 18:33:05 瀏覽:675
inf12編譯器 發布:2024-04-25 18:15:39 瀏覽:99
撲克總督3安卓哪裡下載 發布:2024-04-25 18:10:02 瀏覽:395
什麼網站是php 發布:2024-04-25 18:03:42 瀏覽:221
java教程免費下載 發布:2024-04-25 18:02:01 瀏覽:443
i西安編程 發布:2024-04-25 16:55:35 瀏覽:263
核磁看壓縮 發布:2024-04-25 16:37:22 瀏覽:432
訪問不上光貓 發布:2024-04-25 16:13:44 瀏覽:319