C語言實(shí)現(xiàn)鏈表與文件存取的示例代碼
本程序主要功能是建立鏈表,然后把鏈表數(shù)據(jù)存儲(chǔ)到文件中,然后把文件數(shù)據(jù)存儲(chǔ)到數(shù)組中并輸出。
不多說了,放代碼。
此處為main函數(shù)的內(nèi)容
int main(void)
{
char filename[50];
printf("How many ?: ");
scanf("%d", &n); /*輸入學(xué)生數(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é)點(diǎn),新節(jié)點(diǎn)
pend=phead =(stu*)malloc(sizeof(stu));//分配內(nèi)存給首節(jié)點(diǎn)
printf("please first input Name, Adress and telephone:\n");
for(int i=0;i<n;i++)
{
pnew=(stu*)malloc(sizeof(stu)); //分配新節(jié)點(diǎn)
pend->pnext=pnew; //原來的尾節(jié)點(diǎn)指向新節(jié)點(diǎn)
pnew->pnext=NULL; //新節(jié)點(diǎn)的指針為NULL
printf("NO.%d: ",i+1);
scanf("%s", pend->name);
scanf("%s", pend->adr);
scanf("%d",&pend->tel);
pend=pnew; //賦值后指向尾節(jié)點(diǎn)
}
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){ /*二進(jìn)制只寫打開文件*/
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;//指向下一個(gè)節(jié)點(diǎn)
}
fclose(w); //關(guān)閉文件
}
三、輸出文件
先把文件內(nèi)容保存到結(jié)構(gòu)體數(shù)組內(nèi),然后再通過數(shù)組輸出到屏幕上。
代碼如下:
void show(char *filename) //輸出文件
{
FILE *fp;//文件指針
stu info[100]; //負(fù)責(zé)存放文件中的數(shù)據(jù),然后輸出
if ((fp = fopen(filename, "rb")) == NULL){ /*二進(jìn)制只讀打開文件*/
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】的改進(jìn)版*/
#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é)點(diǎn),新節(jié)點(diǎn)
pend=phead =(stu*)malloc(sizeof(stu));//分配內(nèi)存給首節(jié)點(diǎn)
printf("please first input Name, Adress and telephone:\n");
for (int i = 0; i < n; i++)
{
pnew=(stu*)malloc(sizeof(stu)); //分配新節(jié)點(diǎn)
pend->pnext=pnew; //原來的尾節(jié)點(diǎn)指向新節(jié)點(diǎn)
pnew->pnext=NULL; //新節(jié)點(diǎn)的指針為NULL
printf("NO.%d: ",i+1);
scanf("%s", pend->name);//輸入數(shù)據(jù)存到鏈表中
scanf("%s", pend->adr);
scanf("%d",&pend->tel);
pend=pnew; //賦值后指向尾節(jié)點(diǎn)
}
pnew=pnew->pnext;//指向NULL
free(pnew); //釋放pnew內(nèi)存
}
void save(char *filename)/*存到文件內(nèi)*/
{
FILE *w;//文件指針
if ((w = fopen(filename, "wb")) == NULL){ /*二進(jìn)制只寫打開文件*/
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;//指向下一個(gè)節(jié)點(diǎn)
}
fclose(w); //關(guān)閉文件
}
void show(char *filename)/*輸出文件*/
{
FILE *fp;//文件指針
stu info[100]; //負(fù)責(zé)存放文件中的數(shù)據(jù),然后輸出
if ((fp = fopen(filename, "rb")) == NULL){ /*二進(jìn)制只讀打開文件*/
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); /*輸入學(xué)生數(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語言實(shí)現(xiàn)鏈表與文件存取的示例代碼的文章就介紹到這了,更多相關(guān)C語言鏈表與文件存取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
函數(shù)指針的強(qiáng)制類型轉(zhuǎn)換實(shí)現(xiàn)代碼
函數(shù)指針的強(qiáng)制類型轉(zhuǎn)換實(shí)現(xiàn)代碼。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-10-10
C++實(shí)現(xiàn)LeetCode(347.前K個(gè)高頻元素)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(347.前K個(gè)高頻元素),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
C語言實(shí)現(xiàn)動(dòng)態(tài)版通訊錄的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C語言實(shí)現(xiàn)一個(gè)簡單的動(dòng)態(tài)版通訊錄,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C語言有一定幫助,需要的可以參考一下2022-08-08
C++實(shí)現(xiàn)LeetCode(163.缺失區(qū)間)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(163.缺失區(qū)間),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++使用ffmpeg實(shí)現(xiàn)rtsp取流的代碼
這篇文章主要介紹了C++使用ffmpeg實(shí)現(xiàn)rtsp取流,文章介紹了ffmepg采用rtsp取流流程圖,CMakeLists.txt編寫方法,通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04

