C語言文件操作與相關(guān)函數(shù)介紹
1.操作的準備
c語言中的文件操作相關(guān)函數(shù)的頭文件都是在#include<stdio.h>里
除此之外還得定義一個文件指針對目標文件進行指向操作,一般形式為:FILE *指針變量名;
例如:FILE *fp
2.文件的打開
對于數(shù)據(jù)的處理一般有兩種存放形式:文本文件和二進制文件
想要操作文件,那肯定先得把它打開,而在C語言中一般使用fopen函數(shù)打開文件
一般形式為:fopen(文件名,文件操作形式);
該函數(shù)成功實現(xiàn)將返回一個文件指針,失敗則返回NULL
例如:
FILE* pf = fopen("test.txt", "w");
一般為了保證文件正常打開,會進行以下測試:
if (pf == NULL) { perror("fopen"); return 1; }
3.文件的使用方式
使用文件的方式如下:
“r”(只讀):為輸入打開一個文本文件 對文件進行讀操作
“w”(只寫):為輸出打開一個文本文件 對文件進行寫操作
“a”(追加): 向文本文件尾添加數(shù)據(jù)
“rb”(只讀): 為輸入打開有一個二進制文件
“wb”(只寫):為輸出打開一個二進制文件對文件進行寫操作
“ab”(追加): 向二進制文件尾添加數(shù)據(jù)
“r+”(讀寫): 為讀寫打開一個文本文件
“w+”(讀寫):為讀寫建立一個新的文本文件
“a+”(讀寫): 向文本文件尾添加數(shù)據(jù)
“rb+”(讀寫): 為讀寫打開一個二進制文件
“wb+”(讀寫):為讀寫建立一個新的二進制文件
“ab+”(讀寫): 為讀寫打開一個進制文件
4.相關(guān)的函數(shù)
4.1 fputc
從輸入流緩沖區(qū)中取出下一個字符并將字符放入文本文件中
int fputc ( int character, FILE * stream );
樣例:
#include<stdio.h> int main() { FILE* pf = fopen("test.txt.txt", "w"); if (pf == NULL) { perror("fopen"); return 1; } //寫文件 char ch = 'a'; for (ch = 'a'; ch <= 'z'; ch++) { fputc(ch, pf); } fclose(pf); pf = NULL; return 0; }
運行結(jié)果:
4.2 fgetc
向文本文件中寫出一個單個字符
int fgetc ( FILE * stream );
樣例:(文本文件與fputc調(diào)用相同)
#include<stdio.h> int main() { FILE* pf = fopen("test.txt.txt", "r"); if (pf == NULL) { perror("fopen"); return 1; } //讀文件 int ch = 0; while ((ch = fgetc(pf)) != EOF) { printf("%c ", ch); } fclose(pf); pf = NULL; return 0; }
運行結(jié)果:
4.3 fputs
從輸入流緩沖區(qū)中取出下一個字符串并將字符放入文本文件中
int fputs ( const char * str, FILE * stream );
樣例:
int main() { char arr[256] = { 0 }; FILE* pf = fopen("test.txt.txt", "w"); if (pf == NULL) { perror("fopen"); return 1; } //寫文件 fputs("ssssssssssssssssss\n", pf); fputs("llllllllllllllllll\n", pf); fclose(pf); pf = NULL; return 0; }
運行結(jié)果:
4.4 fgets
向文本文件中寫出一個字符串
char * fgets ( char * str, int num, FILE * stream );
樣例:(文本文件與fputs調(diào)用相同)
int main() { char arr[256] = { 0 }; FILE* pf = fopen("test.txt.txt", "r"); if (pf == NULL) { perror("fopen"); return 1; } fgets(arr, 255, pf); printf("%s", arr); fgets(arr, 255, pf); printf("%s", arr); fclose(pf); pf = NULL; return 0; }
運行結(jié)果:
4.5 fprintf
格式化輸出到一個流文件中
int fprintf ( FILE * stream, const char * format, ... );
樣例:
struct s { char name[20]; int age; double score; }; #include<stdio.h> int main() { struct s s = { "霧都",20,98.5 }; FILE* pf = fopen("test.txt.txt", "w"); if (pf == NULL) { perror("fopen"); return 1; } //寫文件 fprintf(pf, "%s %d %lf", s.name, s.age, s.score); fclose(pf); pf = NULL; return 0; }
運行結(jié)果:
4.6 fscanf
用于讀取數(shù)據(jù)
int fscanf ( FILE * stream, const char * format, ... );
樣例:(文本文件與fprintf調(diào)用相同)
struct s { char name[20]; int age; double score; }; #include<stdio.h> int main() { struct s s = { 0 }; FILE* pf = fopen("test.txt.txt", "r"); if (pf == NULL) { perror("fopen"); return 1; } fscanf(pf, "%s %d %lf", s.name, &(s.age), &(s.score)); printf("%s %d %lf", s.name, s.age, s.score); /*fprintf(stdout, "%s %d %lf", s.name, s.age, s.score);*/ fclose(pf); pf = NULL; return 0; }
運行結(jié)果:
4.7 fwrite
將 count 個大小為 length 的對象從名為 buffer 的數(shù)組二進制寫入輸入流文件名。
size_t fwrite(void *buffer, size_t length, size_t count, FILE *filename);
樣例:
#include<stdio.h> struct s { char name[20]; int age; double score; }; int main() { struct s s = { "霧都",20,98.5 }; FILE* pf = fopen("test.txt.txt", "w"); if (pf == NULL) { perror("fopen"); return 1; } //二進制的寫 fwrite(&s, sizeof(struct s), 2, pf); fclose(pf); return 0; }
運行結(jié)果:(因為是轉(zhuǎn)成二進制了,倒也正常)
4.8 fread
二進制讀取count
大小對象的數(shù)量length
從輸入流filename
到名為的數(shù)組buffer
size_t fread(void *buffer, size_t length, size_t count, FILE *filename);
樣例:(二進制文件與fwrite調(diào)用相同)
struct s { char name[20]; int age; double score; }; int main() { struct s s = { "霧都",20,98.5 }; FILE* pf = fopen("test.txt.txt", "r"); if (pf == NULL) { perror("fopen"); return 1; } //二進制的讀 fread(&s, sizeof(struct s), 2, pf); printf("%s %d %lf", s.name, s.age, s.score); fclose(pf); return 0; }
運行結(jié)果:
到此這篇關(guān)于C語言文件操作與相關(guān)函數(shù)介紹的文章就介紹到這了,更多相關(guān)C語言文件操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實現(xiàn)寢室衛(wèi)生管理系統(tǒng)
這篇文章主要為大家詳細介紹了C++實現(xiàn)寢室衛(wèi)生管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03C語言 動態(tài)內(nèi)存開辟常見問題解決與分析流程
動態(tài)內(nèi)存是相對靜態(tài)內(nèi)存而言的。所謂動態(tài)和靜態(tài)就是指內(nèi)存的分配方式。動態(tài)內(nèi)存是指在堆上分配的內(nèi)存,而靜態(tài)內(nèi)存是指在棧上分配的內(nèi)存2022-03-03C語言實現(xiàn)宿舍管理系統(tǒng)設(shè)計
這篇文章主要為大家詳細介紹了C語言實現(xiàn)宿舍管理系統(tǒng)設(shè)計,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03