zoukankan      html  css  js  c++  java
  • [05]APUE:标准 I/O 库

    [a] setvbuf / setbuf

    #include <stdio.h>
    int setvbuf(FILE *restrict fp, char *restrict buf, int mode, size_t size) //成功返回 0,出错返回非 0
    void setbuf(FILE *restrict fp, char *restrict buf) 
    • mode:_IONBF / _IOLBF / _IOFBF,即无缓冲、行缓冲、全缓冲
    • setvbuf:若 mode 为 _IONBF,忽略 buf 与 size 的值;若 buf 为 NULL 且为缓冲模式,可不指定 size,系统将自动分配
    • setbuf 通过将 buf 设置为 NULL 或 BUFSIZ 关闭或开启缓冲,缓冲的模式由系统根据连接的对象自动选择

    [b] fflush

    #include <stdio.h>
    int fflush(FILE *fp) //成功返回 0,出錯返回 EOF 
    • 手动冲洗待输出的标准 I/O 缓冲,对输入方向的缓冲无效

    [c] fopen / fdopen / freopen

    #include <stdio.h>
    FILE *fopen(const char *restrict path, const char *restrict type)
    FILE *fdopen(int fd, const char *type)
    FILE *freopen(const char *path, const char *type, FILE *fp) 
    //成功返回文件指针,出错返回 NULL 
    • type:r / w / a / r+ / w+ / a+,对应的 open 标志分别为 O_RDONLY / O_WRONLY|O_CREAT|O_TRUNC / O_WRONLY|O_CREAT|O_APPEND / O_RDWR / O_RDWR|O_CREAT|O_TRUNC / O_RDWR|O_CREAT|O_APPEND
    • fdopen 基于已打开的文件描述符生成流指针
    • freopen 用于重定向,以新打开的流指针取代目标 fp(如 stdout / stdin 等)

    [d] fclose

    #include <stdio.h>
    int fclose(FILE *fp) //成功返回 0,出错返回 EOF 
    • 冲洗待输出数据,丢弃所有输入数据,关闭流指针

    [e] fgetc / getc / getchar

    #include <stdio.h>
    int fgetc(FILE *fp)
    int getc(FILE *fp)
    int getchar(void)
    //成功返回下一个字符,到达文件末尾或出错返回 EOF 
    • 用于读取单个字符,getc 实现为宏,getchar 相当于 getc(stdin),fgetc 实现为函数
    • 返回 EOF 时,需要使用 feof 与 ferror 区分到达文件末尾与出错两种状态

    [f] ferror / feof / clearerr

    #include <stdio.h>
    int ferror(FILE *fp)
    int feof(FILE *fp)
    //若条件为真,返回非 0,否则返回 0
    void clearerr(FILE *fp) 
    • clearerr 用于清除 EOF 及错误标志

    [g] ungetc

    #include <stdio.h>
    int ungetc(int c, FILE *fp) //成功返回字符 c,出错返回 EOF 
    • 将指定字符压送回流中

    [h] fputc / putc / putchar

    #include <stdio.h>
    int fputc(char c, FILE *fp)
    int putc(char c, FILE *fp)
    int putchar(char c)
    //成功返回字符 c,出错返回 EOF 
    • putchar 等同于 putc(stdout),fputc 则实现为函数

    [i] fgets / fputs

    #include <stdio.h>
    char *fgets(char *restrict buf, int n, FILE *restrict fp) //成功返回 buf,到达文件尾或出错返回 NULL
    int fputs(const char *restrict str, FILE *restrict fp) //成功返回非负值,出错返回 EOF 
    • fgets 从流中每次读取一行至 buf 中,至多读取 n - 1 个字符,以 字符结束,遇到 则停止读取, 会被读入
    • fputs 将一个字符串写入到流中,不会自动添加 , 字符不会写出

    [j] ftell / fseek / rewind

    #include <stdio.h>
    long ftell(FILE *fp) //成功返回当前位置,出错返回 -1
    int fseek(FILE *fp, long offset, int whence) //成功返回 0,出错返回 -1
    void rewind(FILE *fp) 
    • whence 的值可以为 SEEK_CUR / SEEK_SET / SEEK_END
    • rewind 用于将流指针位置重置到文件开头

    [k] fprintf / snprintf

    #include <stdio.h>
    int fprintf(FILE *restrict fp, const char *restrict format, ...) //返回成功输出的不包括结尾  的字符数量
    int snprintf(char *restrict buf, size_t n, const char *restrict format, ...) //同上 
    • snprintf 用于将至多 n - 1 个字符写入到 buf 中
    • format 格式:'-' 减号表示左对齐(默认右对齐),'+' 加号表示显示正负号,‘LF’ 指 long double,‘lld’ 指 long long int,‘z’ 指 size_t 类型,'j' 指 intmax_t 或 uintmax_t

    [l] fscanf / sscanf 

    #include <stdio.h>
    int fscanf(FILE *restrict fp, const char *restrict format, ...)
    int sscanf(const char *restrict buf, const char *restrict format, ...)
    //成功返回输入的项数,出错或到达文件末尾返回 EOF

    [m] fileno

    #include <stdio.h>
    int fileno(FILE *fp) //返回与该流相关联的文件描述符

    [n] mkdtemp / mkstemp

    #include <stdlib.h>
    char *mkdtemp(char *template) //成功返回指向临时目录的指针,出错返回 NULL
    int mkstemp(char *template) //成功返回临时文件的描述符,出错返回 -1
    • template 必须是以 ‘XXXXXX’ 结尾的可读写字符串(数组),函数执行之后,template 的值会更新为实际的目录名或文件名
    • 临时目录的权限是 0700,临时文件的权限是 0600
    • 临时文件创建完成后处于打开状态,且不会自动删除,需要手动 unlink
  • 相关阅读:
    招聘.NET开发人员
    SQL 2005 SSIS 导入数据效率问题
    用户控件使用事件与调用页面交互
    使用sql语句删除标识列属性
    poj1520
    poj1476
    poj1363
    poj1477
    poj1312
    大端法小端法与union
  • 原文地址:https://www.cnblogs.com/hadex/p/6188558.html
Copyright © 2011-2022 走看看