APUE第五章:标准IO库

5.2流和file对象

#include <wchar.h>
int fwide(FILE *fp, int mode);
Returns: positive if stream is wide oriented,negative if stream is byte oriented,or 0 if stream has no orientation
  • mode 负:字节定向,mode 正:宽字节定向, mode 0:不设置流定向但是返回流定向值

5.4缓冲

IO缓冲方式:全缓冲(缓冲区满存入磁盘),行缓冲(收到换行符存入磁盘),不带缓冲
  1. 标准错误不缓冲
  2. 终端设备行缓冲
  3. 其他全缓冲
#include <stdio.h>
void setbuf(FILE *restrict fp, char *restrict buf );
int setvbuf(FILE *restrict fp, char *restrict buf, int mode,size_t size);
        Returns: 0 if OK, nonzero on error
fp: 已打开的文件指针
buf:未未指定缓冲的流指定缓冲区
size: 制定的缓冲区大小
mode:
 * _IOFBF   全缓冲
 * _IOLBF   行缓冲
 * _IONBF   不缓冲

5.5 打开流

#include <stdio.h>
FILE *fopen(const char *restrict pathname, const char *restrict type);
FILE *freopen(const char *restrict pathname, const char *restrict type,FILE *restrict fp);
FILE *fdopen(int fd, const char *type);
All three return: file pointer if OK, NULL on error
  • fopen 指定方式读取pathname路径名指定的文件
  • freopen 指定流上打开指定文件
  • fdopen 关联一和IO流和一个指定文件指针

5.6 读写流

#include <stdio.h>
int getc(FILE *fp);
int fgetc(FILE *fp);
int getchar(void);
All three return: next character if OK, EOF on end of file or error
#include <stdio.h>
int ferror(FILE *fp);
int feof(FILE *fp);
Both return: nonzero (true) if condition is true, 0 (false) otherwise
void clearerr(FILE *fp);
  • 每个问价对象维护一个出错标志和一个结束标志,clearerr清除这两个标志
#include <stdio.h>
int ungetc(int c, FILE *fp);
        Returns: c if OK, EOF on error
  • 字符压回流中,顺序与读出相反(暂时写回缓冲区,并未写回磁盘)
#include <stdio.h>
int putc(int c, FILE *fp);
int fputc(int c, FILE *fp);
int putchar(int c);
            All three return: c if OK, EOF on error
  • 行读入
#include <stdio.h>
char *fgets(char *restrict buf, int n, FILE *restrict fp);
char *gets(char *buf );
Both return: buf if OK, NULL on end of file or error
int fputs(const char *restrict str, FILE *restrict fp);
int puts(const char *str);
Both return: non-negative value if OK, EOF on error

5.9 二进制IO

#include <stdio.h>
size_t fread(void *restrict ptr, size_t size, size_t nobj,FILE *restrict fp);
size_t fwrite(const void *restrict ptr, size_t size, size_t nobj,FILE *restrict fp);
                            Both return: number of objects read or written
size : object size
nobj : number of object

5.10 定位流

#include <stdio.h>
long ftell(FILE *fp);
                    Returns: current file position indicator if OK, .1L on error
int fseek(FILE *fp, long offset, int whence);
                    Returns: 0 if OK, .1 on error
void rewind(FILE *fp); 流指针返回初始位置
#include <stdio.h>
int fgetpos(FILE *restrict fp, fpos_t *restrict pos);
将文件位置指示器存入pos
int fsetpos(FILE *fp, const fpos_t *pos);
将文件位置指示器设置为pos
            Both return: 0 if OK, nonzero on error

5.11格式化IO

  • output
#include <stdio.h>
int printf(const char *restrict format, ...);
int fprintf(FILE *restrict fp, const char *restrict format, ...);
int dprintf(int fd, const char *restrict format, ...);
                All three return: number of characters output if OK, negative value if output error
int sprintf(char *restrict buf, const char *restrict format, ...);
                Returns: number of characters stored in array if OK, negative value if encoding error
int snprintf(char *restrict buf, size_t n,const char *restrict format, ...);
                Returns: number of characters that would have been stored in array
                    if buffer was large enough, negative value if encoding error
  • input
#include <stdio.h>
int scanf(const char *restrict format, ...);
int fscanf(FILE *restrict fp, const char *restrict format, ...);
int sscanf(const char *restrict buf, const char *restrict format, ...);
            All three return: number of input items assigned,EOF if input error or end of file before any conversion
posted @ 2016-12-01 20:53  zeroArn  阅读(140)  评论(0编辑  收藏  举报