一、打开与关闭
1.1fopen
FILE * fopen ( const char * filename, const char * mode );
打开文件。即采用mode方式,打开filename文件。
其中,mode有如下形式:
"r" | Open a file for reading. The file must exist. |
"w" | Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. |
"a" | Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist. |
"r+" | Open a file for update both reading and writing. The file must exist. |
"w+" | Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. |
"a+" | Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek , rewind ) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist. |
返回值:
成功:文件流指针
失败:NULL
1.2 freopen
FILE * freopen ( const char * filename, const char * mode, FILE * stream );
以不同的打开方式打开文件。
返回值:
成功:流指针
失败:NULL
1.3 fclose
int fclose ( FILE * stream );
关闭文件。即关闭之前打开的stream。
返回值:
成功:0
失败:EOF
二. 读
2.1 fread
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
从stream(文件流)中读取count个大小为size的数据到ptr中。即从文件的当前定位位置开始,读取size*count的数据,存放在ptr指向的内存中。
返回值:读取到的块数。如果返回结果与count不相等,那么可能出现了:读取错误 or 读到文件尾部。
You can use either ferror or feof to check whether an error happened or the End-of-File was reached.
2.2 fgetc
int fgetc ( FILE * stream );
从流中读取一个字符。
返回值:
成功:字符
失败:EOF(可能是读到文件的尾部或者出错)
2.3 fgets
char * fgets ( char * str, int num, FILE * stream );
从stream中读取字符串。正常情况下,从Stream中读取num-1个字符。如果遇到EOF、换行,就结束读取。
返回值:
成功:str
失败:NULL
2.4 fscanf
int fscanf ( FILE * stream, const char * format, ... );
读取格式化的数据。
返回值:
成功:个数(读取成功的)
失败:EOF
三.写
3.1 fwrite
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
写块数据到流中。将ptr指向的count个size大小的数据写入到sttream。
返回值:
成功:count
失败:!=count
3.2 fputc
int fputc ( int character, FILE * stream );
把字符写入stream。写入一个字符,并将stream定位到原来字符位置(即后移一位)
返回值:
成功:character
失败:EOF
3.3 fputs
int fputs ( const char * str, FILE * stream );
将str指定的字符串写入stream。
返回值:
成功:非负数
失败:EOF
3.4 fprintf
int fprintf ( FILE * stream, const char * format, ... );
将格式化数据,写入文件。
返回值:
成功:写入的字节数
失败:负数
四、定位
4.1 fseek
int fseek ( FILE * stream, long int offset, int origin );
重定位流指示器。在stream中,以origin为基准,偏移offset。
其中,offset整数表示正向偏移,负数表示负向偏移。origin可选择如下三值:
SEEK_SET | Beginning of file |
SEEK_CUR | Current position of the file pointer |
SEEK_END | End of file |
返回值:
定位成功:0
不成功 :非0
4.2 fgetops
int fgetpos ( FILE * stream, fpos_t * position );
获取stream的当前位置。
其中,postion是存储流位置的结构数据。
返回值:
成功:0
失败:!0
4.3 fsetops
int fsetpos ( FILE * stream, const fpos_t * pos );
设置stream的位置指示器。
返回值:
成功:0
失败:!0
/* fsetpos example */ #include <stdio.h> int main () { FILE * pFile; fpos_t position; pFile = fopen ("myfile.txt","w"); fgetpos (pFile, &position); fputs ("That is a sample",pFile); fsetpos (pFile, &position); fputs ("This",pFile); fclose (pFile); return 0; }
五、其他
5.1 fflush
int fflush ( FILE * stream );
清除文件缓冲区,文件以写方式打开时将缓冲区内容写入文件。
返回值:
成功:0
失败:EOF
/* FFLUSH.C */ #include <stdio.h> #include <conio.h> void main( void ) { int integer; char string[81]; /* Read each word as a string. */ printf( "Enter a sentence of four words with scanf: " ); for( integer = 0; integer < 4; integer++ ) { scanf( "%s ", string ); printf( "%s\n ", string ); } /* You must flush the input buffer before using gets. */ fflush( stdin ); printf( "Enter the same sentence with gets: " ); gets( string ); printf( "%s\n ", string ); }
5.2 feof
int feof ( FILE * stream );
检测文件流指示位置是否在尾部。
返回值:
在 :!0
不在:0
5.3 ferror
int ferror ( FILE * stream );
错误检测。
返回值:
有错误:!0
没错误:0
参考资料:
1. stdio.h