本章主要介绍标准I/O库
标准I/O库
流和FILE对象
流的定向分类:
1. 单字节I/O
2. 宽字节I/O
更改流定向:
- 清楚流定向: freopen
- 设置流定向: fwide
1 |
|
若 mode
参数值为负: fwide试图指定流是字节定向
若 mode
参数值为正: fwide试图指定流是宽字节定向的
若 mode
参数值为0: 不设置流的定向
标准输入输出,标准错误
缓冲
stderr : 不带缓冲, 使得出错信息尽快显示出来
若是指向终端设备的流, 则是行缓冲的; 否则是全缓冲的
更改缓冲:
1 |
|
setbuf: 控制开关缓冲 buf 缓冲区长度
setvbuf: 设置缓冲类型
mode:
- _IOFBF 全缓冲
- _IOLBF 行缓冲
- _IONBF 不带缓冲
强行冲洗一个流:
1 |
|
打开流
1 |
|
流的读写
1 | // 输入函数 |
二进制I/O
1 | size_t fread(void *restrict ptr, size_t nobj, FILE *restrict fp); |
定位流
1 | long ftell(*fp); |
格式化I/O
格式化输出
1
2
3
4
5
6
int printf(const char *restrict format);
int fprintf(FILE *restrict fp, const char *restrict format, ...);
int dprintf(int fd, const char *restrict format, ...);
int sprintf(char *restrict buf, const char *restrict format, ... );
int snprintf(char *restrict buf, size_t n, const char *restrict format, ...);Vprintf
格式化输入
1
2
3int scanf(const char *restrict format, ... );
int fscanf(FILE *restrict fp, const *restrict format, ...);
int sscanf(const char *restrict buf, const char *restrict format, ...);获得描述符
1
int fileno(FILE *fp);
临时文件
1 | char *tmpnam(char *ptr); |