C語言實現(xiàn)鏈表與文件存取的示例代碼
本程序主要功能是建立鏈表,然后把鏈表數(shù)據(jù)存儲到文件中,然后把文件數(shù)據(jù)存儲到數(shù)組中并輸出。
不多說了,放代碼。
此處為main函數(shù)的內(nèi)容
int main(void)
{
char filename[50];
printf("How many ?: ");
scanf("%d", &n); /*輸入學生數(shù)*/
printf("please input filename: ");
scanf("%s", filename); /*輸入文件所在路徑及名稱*/
Create(); //調(diào)用函數(shù)建立鏈表
save(filename); //調(diào)用函數(shù)存到文件
free(phead);//釋放phead內(nèi)存
show(filename); //調(diào)用函數(shù)輸出文件
system("pause");
return 0;
}
一、輸入數(shù)據(jù)到鏈表中
建立鏈表并輸入數(shù)據(jù)到鏈表里
代碼如下:
typedef struct stu
{
char name[20];
char adr[20];
int tel;
struct stu* pnext;
} stu;
int n; //n存著信息條數(shù)
stu* phead=NULL;//phead為鏈表首地址
void Create() //建立鏈表
{
stu *pend,*pnew;//尾節(jié)點,新節(jié)點
pend=phead =(stu*)malloc(sizeof(stu));//分配內(nèi)存給首節(jié)點
printf("please first input Name, Adress and telephone:\n");
for(int i=0;i<n;i++)
{
pnew=(stu*)malloc(sizeof(stu)); //分配新節(jié)點
pend->pnext=pnew; //原來的尾節(jié)點指向新節(jié)點
pnew->pnext=NULL; //新節(jié)點的指針為NULL
printf("NO.%d: ",i+1);
scanf("%s", pend->name);
scanf("%s", pend->adr);
scanf("%d",&pend->tel);
pend=pnew; //賦值后指向尾節(jié)點
}
pnew=pnew->pnext;//指向NULL
free(pnew); //釋放pnew內(nèi)存
}二、把鏈表數(shù)據(jù)存入文件
此處用到了fopen、fprintf、fclose等文件操作函數(shù)
代碼如下:
void save(char *filename)
{
FILE *w;//文件指針
if ((w = fopen(filename, "wb")) == NULL){ /*二進制只寫打開文件*/
printf("cannot open file\n");
exit(1);
}
for (int i = 0; i < n; i++) //鏈表數(shù)據(jù)循環(huán)輸入到文件內(nèi)
{
fprintf(w,"%s ",phead->name);
fprintf(w,"%s ",phead->adr);
fprintf(w,"%d", phead->tel);
fprintf(w,"%s","\r\n");//換行
phead=phead->pnext;//指向下一個節(jié)點
}
fclose(w); //關(guān)閉文件
}
三、輸出文件
先把文件內(nèi)容保存到結(jié)構(gòu)體數(shù)組內(nèi),然后再通過數(shù)組輸出到屏幕上。
代碼如下:
void show(char *filename) //輸出文件
{
FILE *fp;//文件指針
stu info[100]; //負責存放文件中的數(shù)據(jù),然后輸出
if ((fp = fopen(filename, "rb")) == NULL){ /*二進制只讀打開文件*/
printf("cannot open file\n");
exit(1);
}
for (int i = 0; i < n; i++)
{
fscanf(fp,"%s",&(info[i].name));//輸出數(shù)據(jù)到數(shù)組
fscanf(fp,"%s",&(info[i].adr));
fscanf(fp,"%d",&(info[i].tel));
printf("%10s%15s%15d\n", info[i].name,
info[i].adr, info[i].tel); //輸出數(shù)據(jù)到屏幕
}
fclose(fp); //關(guān)閉文件
}
完整代碼
/*此代碼為《C語言從入門到精通(第二版)》第十四章(文件)的【例14.7】的改進版*/
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
typedef struct stu
{
char name[20];
char adr[20];
int tel;
struct stu* pnext;
} stu;
int n; //n存著信息條數(shù)
stu* phead=NULL;//phead為鏈表首地址
void Create()/*建立鏈表*/
{
stu *pend,*pnew;//尾節(jié)點,新節(jié)點
pend=phead =(stu*)malloc(sizeof(stu));//分配內(nèi)存給首節(jié)點
printf("please first input Name, Adress and telephone:\n");
for (int i = 0; i < n; i++)
{
pnew=(stu*)malloc(sizeof(stu)); //分配新節(jié)點
pend->pnext=pnew; //原來的尾節(jié)點指向新節(jié)點
pnew->pnext=NULL; //新節(jié)點的指針為NULL
printf("NO.%d: ",i+1);
scanf("%s", pend->name);//輸入數(shù)據(jù)存到鏈表中
scanf("%s", pend->adr);
scanf("%d",&pend->tel);
pend=pnew; //賦值后指向尾節(jié)點
}
pnew=pnew->pnext;//指向NULL
free(pnew); //釋放pnew內(nèi)存
}
void save(char *filename)/*存到文件內(nèi)*/
{
FILE *w;//文件指針
if ((w = fopen(filename, "wb")) == NULL){ /*二進制只寫打開文件*/
printf("cannot open file\n");
exit(1);
}
for (int i = 0; i < n; i++) //鏈表數(shù)據(jù)循環(huán)輸入到文件里
{
fprintf(w,"%s ",phead->name);//數(shù)據(jù)存入到文件
fprintf(w,"%s ",phead->adr);
fprintf(w,"%d", phead->tel);
fprintf(w,"%s","\r\n");//換行
phead=phead->pnext;//指向下一個節(jié)點
}
fclose(w); //關(guān)閉文件
}
void show(char *filename)/*輸出文件*/
{
FILE *fp;//文件指針
stu info[100]; //負責存放文件中的數(shù)據(jù),然后輸出
if ((fp = fopen(filename, "rb")) == NULL){ /*二進制只讀打開文件*/
printf("cannot open file\n");
exit(1);
}
for (int i = 0; i < n; i++)
{
fscanf(fp,"%s",&(info[i].name));//輸出數(shù)據(jù)到數(shù)組
fscanf(fp,"%s",&(info[i].adr));
fscanf(fp,"%d",&(info[i].tel));
printf("%10s%15s%15d\n", info[i].name,
info[i].adr, info[i].tel);//輸出數(shù)據(jù)到屏幕
}
fclose(fp);/*關(guān)閉文件*/
}
int main(void)
{
char filename[50];
printf("How many ?:\n");
scanf("%d", &n); /*輸入學生數(shù)*/
printf("please input filename: ");
scanf("%s", filename); /*輸入文件所在路徑及名稱*/
Create(); //調(diào)用函數(shù)建立鏈表
save(filename); //調(diào)用函數(shù)存到文件
free(phead);//釋放phead內(nèi)存
show(filename); //調(diào)用函數(shù)輸出文件
system("pause");
return 0;
}我嘗試過fread和fwrite的做法,但都失敗了。
到此這篇關(guān)于C語言實現(xiàn)鏈表與文件存取的示例代碼的文章就介紹到這了,更多相關(guān)C語言鏈表與文件存取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
函數(shù)指針的強制類型轉(zhuǎn)換實現(xiàn)代碼
函數(shù)指針的強制類型轉(zhuǎn)換實現(xiàn)代碼。需要的朋友可以過來參考下,希望對大家有所幫助2013-10-10
C++實現(xiàn)LeetCode(347.前K個高頻元素)
這篇文章主要介紹了C++實現(xiàn)LeetCode(347.前K個高頻元素),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08
C++實現(xiàn)LeetCode(163.缺失區(qū)間)
這篇文章主要介紹了C++實現(xiàn)LeetCode(163.缺失區(qū)間),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07

