C语言文件操作函数
C语言的标准输入输出库里有一组使用缓冲机制来操作文件的函数,功能强大,操作方便,是操作文件的首选。
应该重点掌握的函数如下:
fopen 打开文件
fclose 关闭文件
fread 读文件
fgets 读数据到一个字符串缓冲区
fgetc 读一个字符
fwrite 写文件
fputs 将一个字符串写入文件
fputc 将一个字符写入文件
fseek 定位文件指针
rewind 文件指针回到文件首端
ftell 获得文件指针位置
feof 检查是否到达文件尾部
fflush 把缓冲区的数据写到磁盘
1、fopen打开文件
函数原型:
FILE * fopen ( const char * filename, const char * mode );
参数说明:
filename文件名,可以是相对路径(当前程序)或绝对路径
mode打开模式
“r” 打开文件来读,文件必须存在
“w” 打开文件来写,如果文件存在,则原来的内容被清空
“a” 追加内容,如果文件不存在,会自动创建
“r+” 打开文件来读或写,文件必须存在
“w+” 打开文件来读或写,如果文件存在,则原来的内容被清空
“a+” 打开文件来读或追加,读数据可以在文件任何位置,但写的数据一定在文件尾部写入,如果文件不存在,会自动创建
“b” 以二进制方式读写文件,默认情况为文本方式读写文件
/* fopen example */ #include <stdio.h> int main () { FILE * pFile; pFile = fopen ("myfile.txt","w"); if (pFile!=NULL) { fputs ("fopen example",pFile); fclose (pFile); } return 0; } |
2、fread读文件
函数原型:
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
参数:
ptr 存放数据的缓冲区地址
size 数据大小
count 多少段数据大小
stream 文件指针,即fopen的返回值
例子:
/* fread example: read a complete file */ #include <stdio.h> #include <stdlib.h>
int main () { FILE * pFile; long lSize; char * buffer; size_t result;
pFile = fopen ( "myfile.bin" , "rb" ); if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
// 获得文件大小 fseek (pFile , 0 , SEEK_END); lSize = ftell (pFile); rewind (pFile);
// 动态分配内存 buffer = (char*) malloc (sizeof(char)*lSize + 1); if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
// 读取数据到缓冲区 result = fread (buffer,1,lSize,pFile); if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
/* 具体操作 */
fclose (pFile); free (buffer); return 0; } |
3、fgets 读取一个字符串
函数原型:
char * fgets ( char * str, int num, FILE * stream );
参数:
str 存放读取到的数据的缓冲区地址
num 要读取的最大字符数(包含空字符),如果遇到换行符或者文件结尾标志,自动结束
stream 文件指针
例子:
/* fgets example */ #include <stdio.h>
int main() { FILE * pFile; char mystring [100];
pFile = fopen ("myfile.txt" , "r"); if (pFile == NULL) perror ("Error opening file"); else { fgets (mystring , 100 , pFile); puts (mystring); fclose (pFile); } return 0; } |
注意:读取100个字符,实际上从文件只能读取到99个有效字符,因为最后的空字符需要占一个位置。
4、fgetc 读取一个字符
函数原型:
int fgetc ( FILE * stream );
参数:
stream 文件指针
返回值:
返回读取到的字符的ASCII值。如果读到文件结尾标志或者读数据错误,返回EOF。到底是读到文件标志还是读数据出现错误,可以通过ferror或者feof取检测。
例子:
/* fgetc example: money counter */ #include <stdio.h> int main () { FILE * pFile; int c; int n = 0; pFile=fopen ("myfile.txt","r"); if (pFile==NULL) perror ("Error opening file"); else { do { c = fgetc (pFile); if (c == '$') n++; } while (c != EOF); fclose (pFile); printf ("The file contains %d dollar sign characters ($)./n",n); } return 0; } |
注意:fgetc读取到最后字符后,结尾标志还没有设定。
5、fwrite写文件
函数原型:
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
参数:
ptr 要写入文件的数据的地址
size 数据大小
count 多少段数据大小
stream 文件指针,即fopen的返回值
例子:
/* fwrite example : write buffer */ #include <stdio.h>
int main () { FILE * pFile; char buffer[] = { 'x' , 'y' , 'z' }; pFile = fopen ( "myfile.bin" , "wb" ); fwrite (buffer , 1 , sizeof(buffer) , pFile ); fclose (pFile); return 0; } |
6、fputs讲一个字符串写入文件
函数原型:
int fputs ( const char * str, FILE * stream );
7、fputc讲一个字符写入文件
函数原型:
int fputc ( int character, FILE * stream );
8、fseek定位文件指针
函数原型:
int fseek ( FILE * stream, long int offset, int origin );
参数:
stream 文件指针
offset 相对于文件指针位置的偏移量,向后为正直,向前为负值
origin 文件指针位置
SEEK_SET 文件开端
SEEK_CUR 当前位置
SEEK_END 文件结尾
例子:
/* fseek example */ #include <stdio.h>
int main () { FILE * pFile; pFile = fopen ( "example.txt" , "w" ); fputs ( "This is an apple." , pFile ); fseek ( pFile , 9 , SEEK_SET ); fputs ( " sam" , pFile ); fclose ( pFile ); return 0; } |
9、rewind文件指针回到文件开端
函数原型:
void rewind ( FILE * stream );
10、ftell获得当前文件指针距离文件开端的长度
函数原型:
long int ftell ( FILE * stream );
例子:
fseek (pFile , 0 , SEEK_END); lSize = ftell (pFile); rewind (pFile); |
11、feof 检查是否到达文件尾部
函数原型:
int feof ( FILE * stream );
参数:
stream 文件指针
返回值:
如果到达文件尾部,返回非零值,否则返回零。
例子:
/* feof example: byte counter */ #include <stdio.h> int main () { FILE * pFile; long n = 0; pFile = fopen ("myfile.txt","rb"); if (pFile==NULL) perror ("Error opening file"); else { while (!feof(pFile)) { fgetc (pFile); n++; } fclose (pFile); printf ("Total number of bytes: %d/n", n-1); } return 0; } |
12、fflush将当前缓冲区的数据立即写到磁盘
函数原型:
int fflush ( FILE * stream );
例子:
/* fflush example */ #include <stdio.h> char mybuffer[80]; int main() { FILE * pFile; pFile = fopen ("example.txt","r+"); if (pFile == NULL) perror ("Error opening file"); else { fputs ("test",pFile); fflush (pFile); // flushing or repositioning required fgets (mybuffer,80,pFile); puts (mybuffer); fclose (pFile); return 0; } } |