Linux 文件流管理

1. 打开/关闭文件

1). 打开文件 / fopen

  • 作用
    打开一个文件,将其与文件流联系起来,方便后续的操作

  • 头文件

        #include <stdio.h>  
    
  • 函数原型

        FILE *fopen(const char *file_name, const char *mode)
    
  • 参数

  • file_name: 欲打开的文件名(可包含路径)

  • mode:

       r(rb):      只读的方式打开文件  (该文件必须存在)
       r+(rb+):    以读&写的方式打开文件   (该文件必须存在)
       w(wb):      只写的方式打开文件  (若不存在,则创建该文件)
       w+(wb+):    以读&写的方式打开文件   (若不存在,则创建该文件)
       a(ab):      以只写的方式追加,若文件存在,则追加数据;若不存在,则创建文件
       a+(ab+):    以读&写的方式追加,若文件存在,则追加数据;若不存在,则创建文件
    
  • 返回值
    成功:返回文件指针
    失败:返回 NULL
    错误码:见errno

  • 相似函数

        FILE *fdopen(int fd, const char *mode)  //POSIX函数,通过文件描述符打开文件
        FILE *freopen(const char *path, const char *mode, FILE *stream)     //重新打开某个文件(相当于重启)
    

2). 关闭文件 / fclose

  • 作用
    关闭一个已经打开的文件

  • 头文件

        #include <stdio.h>
    
  • 函数原型

        int fclose(FILE *fp)
    
  • 参数

  • fp: fopen打开文件时候返回的流指针(FILE *)
  • 返回值
    成功:0
    失败:EOF (End Of File)

2. 读/写文件流

ASCI C 提供了3种不同的I/O处理函数:

  • 字符读/写

fgetc / getc / getchar
fputc / putc

  • 行读/写

fgets / gets
fputs / puts

  • 块读/写

fread
fwrite

1). 字符读/写

①. fgetc / getc / getchar

  • 作用
    读取文件流中的字符并返回

  • 头文件

        #include <stdio.h>
    
  • 函数原型

        int fgetc(FILE *stream)
        int getc(FILE *stream)      //同fgetc
        int getchar(void)           //是getc(stdin)
    
  • 参数

stream: fopen打开文件时候返回的流指针(FILE *)

  • 返回值
    成功:返回读取的字符(以其ACSII码的形式)
    失败:EOF

②. fputc / putc / putchar

  • 作用
    关闭一个已经打开的文件

  • 头文件

        #include <stdio.h>
    
  • 函数原型

        int fputc(int c, FILE *stream)
        int putc(int c, FILE *stream)       //same to fputc
        int putchar(int c);                 //same as putc(c, stdout)
    
  • 参数

stream: fopen打开文件时候返回的流指针(FILE *)

  • 返回值
    成功:返回读取的字符(以其ACSII码的形式)
    失败:EOF

2). 行读/写

①. fgets / gets

  • 作用
    从文件流中读取一行字符串

  • 头文件

        #include <stdio.h>
    
  • 函数原型

        char *fgets(char *s, int size, FILE *stream)  
        char *gets(char *s)
    
  • 参数

  • s:存储读取字符串的buffer
  • stream: fopen打开文件时候返回的流指针(FILE *)
  • 返回值
    成功:读取到的字符串
    失败:NULL

②. fputs / puts

  • 作用
    向流中写入一行字符串

  • 头文件

        #include <stdio.h>
    
  • 函数原型

        int fputs(const char *s, FILE *stream)  
        int puts(const char *s)
    
  • 参数

  • s:欲输出的字符串
  • stream: fopen打开文件时候返回的流指针(FILE *)
  • 返回值
    成功:返回字符串的字符个数(sizeof("xxx"))
    失败:EOF

3). 块读/写

①. fread

  • 作用
    从流中读取一定数量的字符

  • 头文件

        #include <stdio.h>
    
  • 函数原型

        size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
    
  • 参数

  • ptr:读取到的数据存储到该指针所执行的内存
  • size:每个数据块的大小
  • nmemb:需要读取多少个字符块
  • stream: fopen打开文件时候返回的流指针(FILE *)
  • 返回值
    成功:返回读取的bytes
    失败:不等于读取的字节数

②. fwrite

  • 作用
    向流中写入一定数量的字符

  • 头文件

        #include <stdio.h>
    
  • 函数原型

        size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
    
  • 参数

  • ptr:读取到的数据存储到该指针所执行的内存
  • size:每个数据块的大小
  • nmemb:需要读取多少个字符块
  • stream: fopen打开文件时候返回的流指针(FILE *)
  • 返回值
    成功:返回写入的bytes
    失败:不等于读取的字节数

3. 文件流检查

当读取/写入文件错误,需要对文件流进行检查,看到底是读取到了文件末尾还是真的发生了错误,有以下几个函数可以采用:

  • feof:
    检查是否到了文件末尾(EOF, End-Of-File)
  • ferror:
    检查文件流是否发生了错误
  • clearerr
    清除所有的文件流错误(包括EOF)
  • fileno
    检查文件流的合法性,并返回与文件流对应的文件描述符fd

1). feof

  • 作用
    检查是否到了文件流的结尾(EOF)

  • 头文件

        #include <stdio.h>
    
  • 函数原型

        int feof(FILE *stream)
    
  • 参数

stream: fopen打开文件时候返回的流指针(FILE *)

  • 返回值
    已到文件尾:非0值
    未到文件尾:0
    失败:默认不允许失败,为-1

2). ferror

  • 作用
    检查文件流是否发生错误

  • 头文件

        #include <stdio.h>
    
  • 函数原型

        int ferror(FILE *stream)
    
  • 参数

stream: fopen打开文件时候返回的流指针(FILE *)

  • 返回值
    成功:非0值
    默认不允许失败,真的失败为-1

3). clearerr

  • 作用
    清除文件流的错误

  • 头文件

        #include <stdio.h>
    
  • 函数原型

        int clearerr(FILE *stream)
    
  • 参数

stream: fopen打开文件时候返回的流指针(FILE *)

  • 返回值
    总是成功,所以无需返回值

4). fileno

  • 作用
    检查文件流是否发生错误

  • 头文件

        #include <stdio.h>
    
  • 函数原型

        int fileno(FILE *stream)
    
  • 参数

stream: fopen打开文件时候返回的流指针(FILE *)

  • 返回值
    成功:文件描述符
    失败:-1

4. 文件流定位

  • ftell:
    返回当前读写位置
  • fseek:
    修改当前读写位置
  • rewind:
    重置当前读写位置

1). ftell

  • 作用
    获取当前的读写位置

  • 头文件

        #include <stdio.h>
    
  • 函数原型

        long ftell(FILE *stream)
    
  • 参数

stream: fopen打开文件时候返回的流指针(FILE *)

  • 返回值
    成功:当前相对文件开头的指针偏移量
    失败:-1

2). fseek

  • 作用
    修改当前的读写位置

  • 头文件

        #include <stdio.h>
    
  • 函数原型

        int fseek(FILE *stream, long offset, int whence)
    
  • 参数

  • stream: fopen打开文件时候返回的流指针(FILE *)
  • offset: 相对于whence的偏移量(可正可负)
  • whence: 文件指针位置

SEEK_SET: 文件起始位置
SEEK_CUR: 文件当前指针位置
SEEK_END:文件结尾位置

  • 返回值
    成功:0
    失败:-1

3). rewind

  • 作用
    将文件流指针指向开始位置

  • 头文件

        #include <stdio.h>
    
  • 函数原型

        void rewind(FILE *stream)
    
  • 参数

stream: fopen打开文件时候返回的流指针(FILE *)

  • 返回值
    无返回值

4). fgetpos / fsetpos

  • 作用
    获取/设置当前文件流指针的位置

  • 头文件

        #include <stdio.h>
    
  • 函数原型

        int fgetpos(FILE *stream, fpos_t *pos)  //same as ftell(stream)
        int fsetpos(FILE *stream, fpos_t *pos)  //same as fseek(stream, pos, SEEK_SET)
    
  • 参数

  • stream: fopen打开文件时候返回的流指针(FILE *)
  • pos: 相对于文件开始的偏移量
  • 返回值
    成功:0
    失败:-1
原文地址:https://www.cnblogs.com/Jimmy1988/p/7485569.html