Skip to content

函数

cpp
FILE *fopen(char *filename,char *mode)

不能正常打开,返回NULL

cpp
exit(0) //结束main函数
fclose(FILE*) //关闭文件

模式

modedescribe
"r"只读read: Open file for input operations. The file must exist.
"w"只写write: Create an empty file for output operations. If a file with the same name already exists, its contents are discarded(清空) and the file is treated as a new empty file.因为会被覆写
"a"追加append: Open file for output at the end of a file. Output operations always write data at the end of the file, expanding it. Repositioning operations (fseekfsetposrewind) are ignored. The file is created if it does not exist.
"r+"读写read/update: Open a file for update (both for input and output). The file must exist.
"w+"写读write/update: Create an empty file and open it for update (both for input and output). If a file with the same name already exists its contents are discarded and the file is treated as a new empty file.
"a+"读写append/update: Open a file for update (both for input and output) with all output operations writing data at the end of the file. Repositioning operations (fseekfsetposrewind) affects the next input operations, but output operations move the position back to the end of file. The file is created if it does not exist.
加b为对二进制文件的操作,例如rb,wb,ab,rb+,wb+,ab+
文件使用模式处理方式指定文件不存在时指定文件存在时
"r"只读(文本文件)出错正常打开
"w"只写(文本文件)建立新文件清除文件原有内容
"a"追加(文本文件)建立新文件在文件原有内容末尾追加
"rb"只读(二进制文件)出错正常打开
"wb"只写(二进制文件)建立新文件文件原有内容丢失
"ab"追加(二进制文件)建立新文件在文件原有内容末尾追加
"r+"读写(文本文件)出错正常打开
"w+"写读(文本文件)建立新文件清除文件原有内容
"a+"读写(文本文件)建立新文件在文件原有内容末尾追加
"rb+"读写(二进制文件)出错正常打开
"wb+"读写(二进制文件)建立新文件清除文件原有内容
"ab+"读写(二进制文件)建立新文件在文件原有内容末尾追加

绝对路径和相对路径

相对路径的起始节点为该源码所处文件下

文件操作

每次使用文件相关函数都会导致文件指针向后移动,因此在不重新配置指针的情况下应当使用rewind等函数更改指针位置以得到正确的结果。

基本读写

文件指针都在参数最后

cpp
int fgetc(FILE *stream);

从指定文件中读一个字符,必须只读/读写模式

cpp
int fputc(int ch,FILE *stream);

为了充分利用缓冲区,提高I/O性能,每次可以写/读一个字符串

cpp
char * fgets ( char * str, int num, FILE * stream );//num:字符串长度
int fputs ( const char * str, FILE * stream );

格式化读写

文件指针在参数最前

fprintf,fscanf
cpp
int fprintf ( FILE * stream, const char * format, ... );//要求文件可写,将格式化内容写入文件
int fscanf ( FILE * stream, const char * format, ... );//要求文件可读

用于从文件中格式化读和格式化输出到文件

随机读写

文件组织结构

文本文件写入内存需要将ASCII码转化为二进制码,写入反之。文本类型文件转换编码需要花费大量时间。 为满足特定需求,文件结构具有记录结构(在C的流中并不实际存在)。 随机读写文件的记录通常具有固定长度,以直接快速访问到指定记录(如数据库)。

符号常量
numnamedes
0SEEK_SET文件首
1SEEK_CUR当前位置
2SEEK_END文件末尾
ftell

获取文件位置指针的当前值,返回与文件首的偏移量。

cpp
long int ftell ( FILE * stream );

把文件指针移到文件末尾,输出ftell的返回值即可计算出文件大小。

fseek

将文件指针移动到指定的位置上

cpp
int fseek ( FILE * stream, long int offset, int origin );//offset:移动字节数,origin:起始点

注意位移量是long型,防止操作>64KB的文件时出错

rewind

将指针置于文件首

cpp
void rewind ( FILE * stream );
二进制数据块读写

文件指针位于最后

fwrite

从内存中取出count个数据块(每个数据块为size个字节),写入到指定的文件中。

cpp
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
//ptr:写入文件的内容在内存中的存放地址(数据块指针)
//size:写入数据块的大小
//count:写入数据块的个数
//stream:写入文件的指针
fread

在指定文件中读count个数据块,放到指定内存单元中去。

cpp
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
feof

检测文件指针是否到达文件末尾。(注意是访问EOF后面的那个位置的时候才会返回true,读取EOF依然是返回false的)

cpp
int feof ( FILE * stream );