C語言文件操作與相關(guān)函數(shù)介紹
1.操作的準(zhǔn)備
c語言中的文件操作相關(guān)函數(shù)的頭文件都是在#include<stdio.h>里
除此之外還得定義一個文件指針對目標(biāo)文件進(jìn)行指向操作,一般形式為:FILE *指針變量名;
例如:FILE *fp
2.文件的打開
對于數(shù)據(jù)的處理一般有兩種存放形式:文本文件和二進(jìn)制文件
想要操作文件,那肯定先得把它打開,而在C語言中一般使用fopen函數(shù)打開文件
一般形式為:fopen(文件名,文件操作形式);
該函數(shù)成功實現(xiàn)將返回一個文件指針,失敗則返回NULL
例如:
FILE* pf = fopen("test.txt", "w");一般為了保證文件正常打開,會進(jìn)行以下測試:
if (pf == NULL)
{
perror("fopen");
return 1;
}3.文件的使用方式
使用文件的方式如下:
“r”(只讀):為輸入打開一個文本文件 對文件進(jìn)行讀操作
“w”(只寫):為輸出打開一個文本文件 對文件進(jìn)行寫操作
“a”(追加): 向文本文件尾添加數(shù)據(jù)
“rb”(只讀): 為輸入打開有一個二進(jìn)制文件
“wb”(只寫):為輸出打開一個二進(jìn)制文件對文件進(jìn)行寫操作
“ab”(追加): 向二進(jìn)制文件尾添加數(shù)據(jù)
“r+”(讀寫): 為讀寫打開一個文本文件
“w+”(讀寫):為讀寫建立一個新的文本文件
“a+”(讀寫): 向文本文件尾添加數(shù)據(jù)
“rb+”(讀寫): 為讀寫打開一個二進(jìn)制文件
“wb+”(讀寫):為讀寫建立一個新的二進(jìn)制文件
“ab+”(讀寫): 為讀寫打開一個進(jìn)制文件
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ù)組二進(jìn)制寫入輸入流文件名。
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;
}
//二進(jìn)制的寫
fwrite(&s, sizeof(struct s), 2, pf);
fclose(pf);
return 0;
}運行結(jié)果:(因為是轉(zhuǎn)成二進(jìn)制了,倒也正常)

4.8 fread
二進(jìn)制讀取count大小對象的數(shù)量length從輸入流filename到名為的數(shù)組buffer
size_t fread(void *buffer, size_t length, size_t count, FILE *filename);
樣例:(二進(jìn)制文件與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;
}
//二進(jìn)制的讀
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++中聲明類的class與聲明結(jié)構(gòu)體的struct關(guān)鍵字詳解
這篇文章主要介紹了C++中聲明類的class與聲明結(jié)構(gòu)體的struct關(guān)鍵字,默認(rèn)情況下結(jié)構(gòu)的所有成員均是公有的,而類的所有成員是私有的,需要的朋友可以參考下2016-01-01
OpenCV圖像旋轉(zhuǎn)Rotate的詳細(xì)介紹
這篇文章主要介紹了OpenCV圖像旋轉(zhuǎn)Rotate,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
C++如何實現(xiàn)BCD碼和ASCII碼的相互轉(zhuǎn)換
這篇文章主要介紹了C++實現(xiàn)BCD碼和ASCII碼互轉(zhuǎn),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
2~62位任意進(jìn)制轉(zhuǎn)換方法(c++)
下面小編就為大家?guī)硪黄?~62位任意進(jìn)制轉(zhuǎn)換方法(c++)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
關(guān)于C++面向?qū)ο笤O(shè)計的訪問性問題詳解
這篇文章主要給大家介紹了關(guān)于C++面向?qū)ο笤O(shè)計的訪問性問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09

