作者:檸檬i,學習C時長兩個月半的個人練習生
第一次寫文章,難免有些不足,請多多包涵,
本程式主要功能是建立鏈表,然后把鏈表資料存盤到檔案中,然后把檔案資料存盤到陣列中并輸出,
不多說了,放代碼,
此處為main函式的內容
int main(void)
{
char filename[50];
printf("How many ?: ");
scanf("%d", &n); /*輸入學生數*/
printf("please input filename: ");
scanf("%s", filename); /*輸入檔案所在路徑及名稱*/
Create(); //呼叫函式建立鏈表
save(filename); //呼叫函式存到檔案
free(phead);//釋放phead記憶體
show(filename); //呼叫函式輸出檔案
system("pause");
return 0;
}
一、輸入資料到鏈表中
建立鏈表并輸入資料到鏈表里
代碼如下:
typedef struct stu
{
char name[20];
char adr[20];
int tel;
struct stu* pnext;
} stu;
int i,n; //i回圈用,n存著資訊條數
stu* phead=NULL;//phead為鏈表首地址
void Create() //建立鏈表
{
stu *pend,*pnew;//尾節點,新節點
pend=phead =(stu*)malloc(sizeof(stu));//分配記憶體給首節點
printf("please first input Name, Adress and telephone:\n");
for(i=0;i<n;i++)
{
pnew=(stu*)malloc(sizeof(stu)); //分配新節點
pend->pnext=pnew; //原來的尾節點指向新節點
pnew->pnext=NULL; //新節點的指標為NULL
printf("NO.%d: ",i+1);
scanf("%s", pend->name);
scanf("%s", pend->adr);
scanf("%d",&pend->tel);
pend=pnew; //賦值后指向尾節點
}
pnew=pnew->pnext;//指向NULL
free(pnew); //釋放pnew記憶體
}
二、把鏈表資料存入檔案
此處用到了fopen、fprintf、fclose等檔案操作函式
代碼如下:
void save(char *filename)
{
FILE *w;//檔案指標
if ((w = fopen(filename, "wb")) == NULL){ /*二進制只寫打開檔案*/
printf("cannot open file\n");
exit(1);
}
for (i = 0; i < 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;//指向下一個節點
}
fclose(w); //關閉檔案
}
三、輸出檔案
先把檔案內容保存到結構體陣列內,然后再通過陣列輸出到螢屏上,
代碼如下:
void show(char *filename) //輸出檔案
{
FILE *fp;//檔案指標
stu info[100]; //負責存放檔案中的資料,然后輸出
if ((fp = fopen(filename, "rb")) == NULL){ /*二進制只讀打開檔案*/
printf("cannot open file\n");
exit(1);
}
for (i = 0; i < n; i++)
{
fscanf(fp,"%s",&(info[i].name));//輸出資料到陣列
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); //輸出資料到螢屏
}
fclose(fp); //關閉檔案
}
以下為完整代碼:

/*此代碼為《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 i,n; //i回圈用,n存著資訊條數
stu* phead=NULL;//phead為鏈表首地址
void Create()/*建立鏈表*/
{
stu *pend,*pnew;//尾節點,新節點
pend=phead =(stu*)malloc(sizeof(stu));//分配記憶體給首節點
printf("please first input Name, Adress and telephone:\n");
for(i=0;i<n;i++)
{
pnew=(stu*)malloc(sizeof(stu)); //分配新節點
pend->pnext=pnew; //原來的尾節點指向新節點
pnew->pnext=NULL; //新節點的指標為NULL
printf("NO.%d: ",i+1);
scanf("%s", pend->name);//輸入資料存到鏈表中
scanf("%s", pend->adr);
scanf("%d",&pend->tel);
pend=pnew; //賦值后指向尾節點
}
pnew=pnew->pnext;//指向NULL
free(pnew); //釋放pnew記憶體
}
void save(char *filename)/*存到檔案內*/
{
FILE *w;//檔案指標
if ((w = fopen(filename, "wb")) == NULL){ /*二進制只寫打開檔案*/
printf("cannot open file\n");
exit(1);
}
for (i = 0; i < 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;//指向下一個節點
}
fclose(w); //關閉檔案
}
void show(char *filename)/*輸出檔案*/
{
FILE *fp;//檔案指標
stu info[100]; //負責存放檔案中的資料,然后輸出
if ((fp = fopen(filename, "rb")) == NULL){ /*二進制只讀打開檔案*/
printf("cannot open file\n");
exit(1);
}
for (i = 0; i < n; i++)
{
fscanf(fp,"%s",&(info[i].name));//輸出資料到陣列
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);//輸出資料到螢屏
}
fclose(fp);/*關閉檔案*/
}
int main(void)
{
char filename[50];
printf("How many ?:\n");
scanf("%d", &n); /*輸入學生數*/
printf("please input filename: ");
scanf("%s", filename); /*輸入檔案所在路徑及名稱*/
Create(); //呼叫函式建立鏈表
save(filename); //呼叫函式存到檔案
free(phead);//釋放phead記憶體
show(filename); //呼叫函式輸出檔案
system("pause");
return 0;
}
我嘗試過fread和fwrite的做法,但都失敗了,
參考文章:
c語言鏈表資料存入檔案和讀取檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/158808.html
標籤:其他
