一、文件編程介紹
文件編程也是C語(yǔ)言里重要的一個(gè)知識(shí)點(diǎn),在電腦上編程肯定是需要與文件、磁盤打交道的。
C語(yǔ)言標(biāo)準(zhǔn)庫(kù)里提供了文件編程接口,我們不需要理解磁盤原理、磁盤驅(qū)動(dòng)、直接調(diào)用C語(yǔ)言提供的接口函數(shù)就可以讀寫磁盤上的文件,非常方便。
一般的文件操作流程就是: 打開文件----讀寫文件-----關(guān)閉文件。
對(duì)應(yīng)起來(lái)就是4個(gè)函數(shù): fopen fread/fwrite fclose
下面就介紹如何使用這4個(gè)函數(shù)完成基本的文件操作。
在嵌入式開發(fā)、單片機(jī)開發(fā)里,也有很多現(xiàn)成的文件系統(tǒng),提供的函數(shù)和C語(yǔ)言標(biāo)準(zhǔn)函數(shù)大同小異,操作流程沒(méi)什么區(qū)別。
二、文件接口函數(shù)介紹
2.1 打開、讀、寫、關(guān)閉函數(shù)原型
這4個(gè)函數(shù)是最基本的函數(shù),只用這4個(gè)已經(jīng)可以完成大部分文件編程需求了。
#include //打開文件 FILE *fopen(const char *path, const char *mode); //讀文件 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); //寫文件 size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream); //關(guān)閉文件 int fclose(FILE *fp);
2.2 寫函數(shù)的基本運(yùn)用
下面的示例代碼演示了如何向文件追加寫數(shù)據(jù)。
#include #include #include int main() { FILE *file; int cnt; /*1. 打開文件*/ file=fopen("D:/123.txt","a+b"); if(file==NULL) { printf("文件打開失敗!\n"); return -1; } /*2. 寫數(shù)據(jù)*/ cnt=fwrite("1234567890",1,10,file); /*3. 關(guān)閉文件*/ fclose(file); printf("cnt=%d\n",cnt); return 0; }
2.3 讀函數(shù)基本運(yùn)用
打開指定文件,讀取100字節(jié)內(nèi)容打印出來(lái)。
#include #include #include int main() { FILE *file; int cnt; char buff[100]; /*1. 打開文件*/ file=fopen("D:/123.txt","rb"); //malloc if(file==NULL) { printf("文件打開失敗!\n"); return -1; } /*2. 寫數(shù)據(jù)*/ cnt=fread(buff,1,100,file); /*3. 關(guān)閉文件*/ fclose(file); //free buff[cnt]='#include #include #include int main() { FILE *file; int cnt; char buff[100]; /*1. 打開文件*/ file=fopen("D:/123.txt","rb"); //malloc if(file==NULL) { printf("文件打開失敗!\n"); return -1; } /*2. 寫數(shù)據(jù)*/ cnt=fread(buff,1,100,file); /*3. 關(guān)閉文件*/ fclose(file); //free buff[cnt]='\0'; printf("%s\n",buff); printf("cnt=%d\n",cnt); return 0; }
'; printf("%s\n",buff); printf("cnt=%d\n",cnt); return 0; }

2.4 文件指針位置偏移 (自動(dòng)向后偏移)
對(duì)文件讀寫時(shí),文件內(nèi)部有一個(gè)文件指針記錄當(dāng)前文件讀寫的位置,可以理解為在電腦上編輯代碼文檔時(shí)看到的光標(biāo)一樣,讀寫時(shí)會(huì)自動(dòng)偏移。
int main() { FILE *file; int cnt; char data; /*1. 打開文件*/ file=fopen("D:/123.txt","rb"); //malloc if(file==NULL) { printf("文件打開失敗!\n"); return -1; } /*2. 讀數(shù)據(jù)---驗(yàn)證文件指針是否可否自動(dòng)向后偏移*/ cnt=fread(&data,1,1,file); printf("data=%c\n",data); cnt=fread(&data,1,1,file); printf("data=%c\n",data); cnt=fread(&data,1,1,file); printf("data=%c\n",data); cnt=fread(&data,1,1,file); printf("data=%c\n",data); cnt=fread(&data,1,1,file); printf("data=%c\n",data); /*3. 關(guān)閉文件*/ fclose(file); //free return 0; }
2.5 設(shè)置文件指針位置
如果想要從指定位置讀取文件,可以調(diào)用fseek函數(shù)定位文件指針的位置。
#include #include #include int main() { FILE *file; int cnt; char data; /*1. 打開文件*/ file=fopen("D:/123.txt","rb"); //malloc if(file==NULL) { printf("文件打開失敗!\n"); return -1; } /*2. 偏移文件指針*/ fseek(file,5,SEEK_SET); /*3. 讀數(shù)據(jù)---驗(yàn)證文件指針是否可否自動(dòng)向后偏移*/ cnt=fread(&data,1,1,file); printf("data=%c\n",data); /*4. 關(guān)閉文件*/ fclose(file); //free return 0; }
2.6 判斷文件讀取完畢
如果要讀取全部文件內(nèi)容,肯定需要知道文件什么 時(shí)候讀取完畢。最常見的判斷方法就是判斷read函數(shù)的返回值,是否與想要讀取的字節(jié)數(shù)相等,不相等要么讀取完畢了要么就是失敗了。
#include #include #include int main() { FILE *file; int cnt; char data; /*1. 打開文件*/ file=fopen("D:/123.txt","rb"); //malloc if(file==NULL) { printf("文件打開失敗!\n"); return -1; } /*2. 偏移文件指針*/ fseek(file,5,SEEK_SET); /*3. 讀數(shù)據(jù)---驗(yàn)證文件指針是否可否自動(dòng)向后偏移*/ while(1) { cnt=fread(&data,1,1,file); if(cnt!=1)break; printf("data=%c\n",data); } /*4. 關(guān)閉文件*/ fclose(file); //free return 0; }
2.7 使用文件操作函數(shù)讀寫結(jié)構(gòu)體數(shù)據(jù)
文件是一個(gè)存儲(chǔ)數(shù)據(jù)的空間,不僅僅可以存儲(chǔ)文本數(shù)據(jù),也可以存儲(chǔ)我們指定的二進(jìn)制數(shù)據(jù)。
向文件里寫入數(shù)據(jù)之后,只要自己知道格式,讀取的時(shí)候按照約定的格式讀取出來(lái)解析就行。
比如: 軟件每次啟動(dòng)時(shí),可以讀取上次保存的配置文件。
//寫結(jié)構(gòu)體數(shù)據(jù) #include #include #include struct MyStruct { int a; int b; char c[100]; }; int main() { FILE *file; int cnt; struct MyStruct stu={666,888,"C語(yǔ)言文件操作學(xué)習(xí)"}; /*1. 打開文件*/ file=fopen("D:/123.txt","wb"); if(file==NULL) { printf("文件打開失敗!\n"); return -1; } /*2. 讀數(shù)據(jù)*/ cnt=fwrite(&stu,1,sizeof(struct MyStruct),file); printf("cnt=%d\n",cnt); /*3. 關(guān)閉文件*/ fclose(file); //free return 0; } //讀結(jié)構(gòu)體數(shù)據(jù) #include #include #include struct MyStruct { int a; int b; char c[100]; }; int main() { FILE *file; int cnt; struct MyStruct stu; /*1. 打開文件*/ file=fopen("D:/123.txt","rb"); if(file==NULL) { printf("文件打開失敗!\n"); return -1; } /*2. 讀數(shù)據(jù)*/ cnt=fread(&stu,1,sizeof(struct MyStruct),file); printf("cnt=%d\n",cnt); printf("%d,%d,%s\n",stu.a,stu.b,stu.c); /*3. 關(guān)閉文件*/ fclose(file); //free return 0; }
C 語(yǔ)言
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。