C++中fflush()函数与rewind()函数

fflush()函数rewind()函数都可以使得scanf()函数屏蔽之前输入的回车符。这两个函数的参数都可以是stdin变量。

1 stdin

stdin一起使用的还有stdoutstderr,这三个变量都是FILE结构的指针,在stdio.h头文件中定义。

stdin是标准的输入流;stdout是标准的输入流;stderr是标准的错误流。默认情况下,stdin从键盘中读取数据,而stdoutstderr将信息在显示器中显示。这三个变量是常量指针,不能对其进行赋值。

2 fflush()函数

该函数的作用是清空在1 stdin”中提到的三种流。

2.1 强制显示输出缓冲区

fflush(stdout);

以上代码的功能是将与输出流关联的缓冲区的内容在屏幕上显示。在每个最后不带\n”的printf()函数后面,最好加上以上代码。因为不带“\n”的printf()函数有可能不会及时的在屏幕上显示。

printf(“Hello World”);

fflush(stdout);


2.2 清空输入缓冲区

fflush(stdin);

以上代码的功能是清空输入缓冲区中的内容。该代码用在scanf()函数之前,用于屏蔽输入缓冲区中的回车符号。

scanf(“%d”, &i);

fflush(stdin);

scanf(“%d”, &j);


3 rewind()函数

也可以是用rewind()函数来清理输入缓冲区中的数据,达到屏蔽回车符号的作用。该函数的使用方法请参考《C++中getchar()的使用方法》


  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
所有的 C / C++ 函数 Constructors (cppstring) Constructors (cppvector) Operators (cppbitset) Operators (cppdeque) Operators (cppstack) Operators (cppstring) Operators (cppvector) abort (stdother) abs (stdmath) acos (stdmath) any (cppbitset) append (cppstring) asctime (stddate) asin (stdmath) assert (stdother) assign (cppdeque) assign (cpplist) assign (cppstring) assign (cppvector) at (cppdeque) at (cppstring) at (cppvector) atan (stdmath) atan2 (stdmath) atexit (stdother) atof (stdstring) atoi (stdstring) atol (stdstring) back (cppdeque) back (cpplist) back (cppqueue) back (cppvector) bad (cppio) begin (cppdeque) begin (cpplist) begin (cppmap) begin (cppmultimap) begin (cppmultiset) begin (cppset) begin (cppstring) begin (cppvector) bsearch (stdother) c_str (cppstring) calloc (stdmem) capacity (cppstring) capacity (cppvector) ceil (stdmath) clear (cppdeque) clear (cppio) clear (cpplist) clear (cppmap) clear (cppmultimap) clear (cppmultiset) clear (cppset) clear (cppvector) clearerr (stdio) clock (stddate) compare (cppstring) copy (cppstring) cos (stdmath) cosh (stdmath) count (cppbitset) count (cppmap) count (cppmultimap) count (cppmultiset) count (cppset) ctime (stddate) data (cppstring) #define (preproc) difftime (stddate) div (stdmath) empty (cppdeque) empty (cpplist) empty (cppmap) empty (cppmultimap) empty (cppmultiset) empty (cpppriorityqueue) empty (cppqueue) empty (cppset) empty (cppstack) empty (cppstring) empty (cppvector) end (cppdeque) end (cpplist) end (cppmap) end (cppmultimap) end (cppmultiset) end (cppset) end (cppstring) end (cppvector) eof (cppio) equal_range (cppmap) equal_range (cppmultimap) equal_range (cppmultiset) equal_range (cppset) erase (cppdeque) erase (cpplist) erase (cppmap) erase (cppmultimap) erase (cppmultiset) erase (cppset) erase (cppstring) erase (cppvector) #error (preproc) exit (stdother) exp (stdmath) fabs (stdmath) fail (cppio)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值