一. fopen/fclose
FILE *fopen(const char *pathname, const char *mode);
功能:打开一个文件,FILE 为一个结构体 开辟的空间在堆区
参数:pathname为文件路经,mode为以什么样的方式打开文件
常用mode: r only read open
r+ only read open and creat file
w only write open
w+ only write open and creat file
返回值:返回一个文件流(相当于指针)
int fclose(FILE *stream);
功能:关闭文件流
二.fread/fwrite
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
功能:从打开的文件中读取文件到ptr指向的空间中
参数:ptr:空间地址,一般定义字符数组,指向数组首地址。
size:每次读取的字节大小。
nmemb:读取个数。
stream:文件流,具体指向的那个文件。
返回值:所读取的数目
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
功能:从ptr指向的空间的数据写入到打开的文件中。
参数:ptr:空间地址,一般定义字符数组,指向数组首地址。
size:每次写入的字节大小。
nmemb:写入个数。
stream:文件流,具体指向的那个文件。
返回值:写入的数目
三.fseek/ftell
int fseek(FILE *stream, long offset, int whence);
long ftell(FILE *stream);
fseek
功能:将打开的文件的指针偏移量进行设置(可理解为光标),fopen 打开以后默认是从第一 个 字 符开始。对文件进行读或者写后指针都会进行偏移。所以fseek()函数可以再次调整指针位置。
参数:offset:相较于当前位置的偏移量
whence:设置指针位置
常用whence:SEEK_SET :指针置于文件开头
SEEK_CUR : 当前位置
SEEK_END:指针置于文件末尾
返回值:偏移量
ftell()
功能:计出文件字节大小
返回值:字节大小