scanf与缓存
来源:互联网 发布:淘宝助理有mac版 编辑:程序博客网 时间:2024/06/06 01:15
转载自http://blog.sina.com.cn/s/blog_7b62c61c0100vhlu.html
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char** argv) {
}
Sets the buffer to be used for I/O operations with the specified stream, which becomes a fully buffered stream, or alternatively, if the argument for buffer is NULL, it disables buffering for the stream, which becomes an unbuffered stream.
This function should be called once the file associated with the stream has already been opened but before any input or output operation has taken place.
意思是说,这个函数是用来设置指定I/O的缓冲区的大小,如果其第二个参数设置为NULL,那么表示对这个流进行相应的I/O操作时,将不再有缓冲,这个函数应该在一个流被打开后,并且在进行任何输入输出之前被调用,而且应该只被调用一次。然后我在测试时,将以上代码修改如下
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char** argv) {
}
完全是按照文档的要求做的,可是并没有达到预期的效果,还是不停的输出"error,input again:",也就是说,这个函数根本就没有起作用,而将代码改为如下形式
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char** argv) {
}
却能起到一定的作用,如果你输入的字符串长度为5,那么会连续输出5条"error,input again:"信息,然后再次要求你输入一个整数,这让我很是莫明其妙,虽然效果不太好,可是这种修改之后,至少不会出现死循环了。
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char** argv) {
}
这样当用户输入的数据类型非法时,内层的while循环就会不停的读输入缓冲,直到读完或收到一个回车换行为止,这样就达到清空输入缓冲的目的了,注意这种方法具有跨平台的特性。
阅读全文
0 1
- scanf与缓存
- scanf与缓存
- scanf与缓存
- scanf与缓存
- scanf输入函数与缓存的关系
- Scanf 请缓存!
- scanf()的缓存问题
- 清除scanf中的缓存
- scanf错误缓存解决方法
- gets()与scanf()
- sscanf 与scanf
- sscanf 与scanf
- sscanf 与scanf
- scanf与缓冲区
- scanf getchar 与 eof
- sscanf 与scanf
- sscanf 与scanf
- printf与scanf
- JAVA语言的三种技术架构
- HDU 1001 Sum Problem JAVA
- 批量提取文件名称
- ListView_itemxml
- HorizontalScrollView加载信息
- scanf与缓存
- echarts实现top可视化效果
- CentOS7挂载ntfs方案
- js学习-函数
- LeetCode-Add Two Numbers
- 洛谷P3121审查黄金 AC自动机+栈
- 暴搜 bzoj1052 覆盖问题
- RTL8723BU WiFi—蓝牙 模块移植
- mainxml