5.2流和file对象
|
|
- mode 负:字节定向,mode 正:宽字节定向, mode 0:不设置流定向但是返回流定向值
5.4缓冲
IO缓冲方式:全缓冲(缓冲区满存入磁盘),行缓冲(收到换行符存入磁盘),不带缓冲
- 标准错误不缓冲
- 终端设备行缓冲
- 其他全缓冲
|
|
5.5 打开流
|
|
- fopen 指定方式读取pathname路径名指定的文件
- freopen 指定流上打开指定文件
- fdopen 关联一和IO流和一个指定文件指针
5.6 读写流
|
|
|
|
每个问价对象维护一个出错标志和一个结束标志,clearerr清除这两个标志
123#include <stdio.h>int ungetc(int c, FILE *fp);Returns: c if OK, EOF on error字符压回流中,顺序与读出相反(暂时写回缓冲区,并未写回磁盘)
|
|
- 行读入1234567#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 errorint 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
|
|
5.10 定位流
|
|
5.11格式化IO
output
12345678910#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 errorint sprintf(char *restrict buf, const char *restrict format, ...);Returns: number of characters stored in array if OK, negative value if encoding errorint snprintf(char *restrict buf, size_t n,const char *restrict format, ...);Returns: number of characters that would have been stored in arrayif buffer was large enough, negative value if encoding errorinput
12345#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