可以FSEEK(标准输入,1,SEEK_SET)或快退(标准输入)被用来刷新输入缓冲区,而不是非便携式fflush(标准输入)? [英] Can fseek(stdin,1,SEEK_SET) or rewind(stdin) be used to flush the input buffer instead of non-portable fflush(stdin)?

查看:470
本文介绍了可以FSEEK(标准输入,1,SEEK_SET)或快退(标准输入)被用来刷新输入缓冲区,而不是非便携式fflush(标准输入)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我发现 fflush(标准输入)不是一个可移植的方式来处理熟悉的问题的换行符潜伏在输入缓冲区的,我一直在使用下面的时候我的不得不使用 scanf函数

Since I discovered fflush(stdin) is not a portable way to deal with the familiar problem of "newline lurking in the input buffer",I have been using the following when I have to use scanf:

while((c = getchar()) != '\n' && c != EOF);

不过今天我碰到这条线,我已经从 cplusplus.com指出绊倒在fflush

fflush()...在开放,更新(即,开放阅读和书面方式)文件,该流应被输出操作之后执行输入操作前冲洗。这是可以做到无论是通过重新定位(fseek的,fsetpos,快退)或致电明确fflush

fflush()...in files open for update (i.e., open for both reading and writting), the stream shall be flushed after an output operation before performing an input operation. This can be done either by repositioning (fseek, fsetpos, rewind) or by calling explicitly fflush

其实,我看过很多times.So之前,我想确认我是否可以简单地使用 scanf函数之前,以下()的人来达到同样的目的是 fflush(标准输入)当它支持服务:

In fact, I have read that before many times.So I want to confirm if I can simply use anyone of the following before the scanf() to serve the same purpose that fflush(stdin) serves when it is supported:

fseek(stdin,1,SEEK_SET);
rewind(stdin);

PS 退(标准输入)似乎pretty安全和可行刷新缓冲区,我错了?

PS rewind(stdin) seems pretty safe and workable to flush the buffer, am I wrong?

错误我应该提到 fseek的(标准输入,0,SEEK_SET)如果我们谈论的是标准输入因为我们不能用任何其他抵消比0或者一个由 FTELL()在这种情况下返回。

Mistake I should have mentioned fseek(stdin,0,SEEK_SET) if we are talking about stdin as we can't use any offset other than 0 or one returned by ftell() in that case.

推荐答案

这是唯一的便携式成语使用方法:

This is the only portable idiom to use:

while((c = getchar()) != '\n' && c != EOF);

若干主题,包括<一个href=\"http://stackoverflow.com/questions/4917801/using-fseek-with-a-file-pointer-that-points-to-stdin\">this解释这个现象,为什么 feesk 通常不会工作。出于同样的原因,我怀疑退将工作要么,其实手册页说,这是等价于:

Several threads including this one explain why feesk won't usually work. for much the same reasons I doubt rewind would work either, in fact the man page says it is equivalent to:

(void) fseek(stream, 0L, SEEK_SET)

这篇关于可以FSEEK(标准输入,1,SEEK_SET)或快退(标准输入)被用来刷新输入缓冲区,而不是非便携式fflush(标准输入)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆