为了在命令行程序中实现和用户的交互,我们编写的程序的运行过程中往往涉及到对标准输入/输出流的多次读写。
[En]
In order to realize the interaction with the user in the command line program, the running process of the program we write often involves reading and writing multiple times to the standard input / output stream.
在C语言中接受用户输入这一块,有着一个老生常谈的问题:"怎么样及时清空输入流中的数据?"
这也是这篇小笔记的主题内容。
先从缓冲区说起。
缓冲区是 内存中划分出来的一部分。通常来说,缓冲区类型有三种:
- 全缓冲
- 行缓冲
- 无缓冲
行缓冲
在C语言中缓冲区这个概念的存在感还是挺强的,比较常用到的缓冲区类型则是 行缓冲了,如标准输入流 stdin
和标准输出流 stdout
一般(终端环境下)就是在行缓冲模式下的。