使用fflush(stdin); [英] usage of fflush(stdin);

查看:83
本文介绍了使用fflush(stdin);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

同时使用scanf两次不同的时间..从键盘缓冲区中删除输入,何时我们应该使用fflush(stdin)以及何时不需要..

例如:

scanf(%d,&a);

scanf(%d,&b);

是这两个scanf之间所需的fflush语句。 。?

并假设我们在第二次扫描的%d之前使用一个标签或一个空格...会一直有效吗?这个空间是否会补偿输入密钥。?

while using scanf two different times..to remove enter from keyboard buffer ,when should we use fflush(stdin) and when is it not required..
for example:
scanf("%d",&a);
scanf("%d",&b);
is an fflush statement required between these two scanf...?
and suppose we use a tab or a space before the %d of the second scanf..will that always work? does that space compensate the enter key.?

推荐答案

不,为什么? fflush 函数以及所有flush方法仅适用于只读或读/写流/文件,它们从缓冲区中写入(提交)所有未写入的数据到文件;这与缓冲有关;换句话说,在调用之后,写入文件的数据与缓冲区同步。这与读取操作完全无关。



你的这个空间是否补偿输入键?不清楚。这个空间与进入无关。您的代码示例也不暗示任何输入。完成输入数据后按Enter键。我必须说所有扫描阅读方法都非常微妙;你需要在你的实际流中有一些与格式字符串完全一致的数据写入;而且,很明显,任何读取操作的结果都取决于之前的 scanf 所做的操作。也许您需要对它进行一些实验,如果仍然不清楚,请描述您想要实现的目标,要实现的数据结构或输入数据的操作顺序。



但我会给你一个简单实用的建议。我根本不会弄乱 scanf 。我们在CodeProject上有其他问题,很多人都很困惑,有充分的理由。相反,我只需按单行输入数据:prompt-enter,prompt-enter ...在每一步中,您输入一整行。你写提示输入这个和那个,然后调用 gets_s 。使用进入整行并按Enter。然后你可以解析使用 sscanf_s 获得的字符串。处理问题,等等。我认为这样的序列更容易理解和维护。



请参阅:

http://en.cppreference.com/w/c/io/gets [ ^ ],

https://msdn.microsoft.com/en-us/library/t6z7bya3.aspx [ ^ ]。



此外,请务必使用所有这些功能的安全性增强变体。例如,不要使用获取 scanf ,使用 gets_s scanf_s ,依此类推。请参阅: https://msdn.microsoft.com/en-us/library/8ef0s5kh.aspx [ ^ ]。



现在,更多关于交互式输入。也许你真的需要全力以赴。但我不会太认真。如果您查看现有的非常有用的仅限控制台的应用程序(例如系统实用程序),它们很少使用交互式输入。这种交互式控制台专用应用程序相对较少使用的类通常提供完全成熟的命令解释器行为。所有其他应用程序都更简单:它们在命令行中一次接受所有输入。如果某些部分数据太大而无法在命令行中写入,则一个或多个参数是文件名,因此使用会在文件中添加更复杂的数据并使用该文件输入数据。像你这样的互动输入对用户来说太不方便了;一个错误可能导致用户重新开始,依此类推。尽量不要沉入其中。



-SA
No, why? The fflush function, as well as all "flush" methods are only for write-only or read/write streams/files, they write (commit) all unwritten data from the buffer to the file; this is related to buffering; in other words, after the call, the data written to the file is synchronized with the buffer. This is totally unrelated to read operations.

Your "does that space compensate the enter key?" is not clear. The space has nothing to do with "enter". Your code sample also does not imply any "enter". You press enter when you complete entering data. I must say that all "scan" methods of reading are pretty delicate; you need to have in your actual stream some data written exactly as expected by your format string(s); moreover, it's apparent that the result of any read operation depends on what was done by the previous scanf. Perhaps you need to experiment with it a bit and, if something is still unclear, describe what exactly you want to achieve, the structure of data to be achieved, or the sequence of the operations on entering data.

But I would give you one simple practical advice. I would not mess with scanf at all. We have other questions here on CodeProject, many people have been much confused, by a good reason. Instead, I would simply enter data by single lines: prompt-enter, prompt-enter… On each step, you get a whole line entered. You write prompts "Enter this and that", and call gets_s. The use enters the whole line and presses "Enter". Then you can parse the string obtained using sscanf_s. Handle the problems, and so on. I think such sequence is much easier to understand and maintain.

Please see:
http://en.cppreference.com/w/c/io/gets[^],
https://msdn.microsoft.com/en-us/library/t6z7bya3.aspx[^].

Also, be sure to use security-enhanced variants of all those functions. For example, don't use gets or scanf, use gets_s and scanf_s, and so on. Please see: https://msdn.microsoft.com/en-us/library/8ef0s5kh.aspx[^].

Now, some more on interactive input. Perhaps you really need to lean it all. But I would not take it too serious. If you look at existing really useful console-only applications, such as system utilities, they rarely use interactive input. The relatively rarely used class of such interactive console-only applications usually provides fully-fledged command interpreter behavior. All other applications are much simpler: they accept all the input at once, in the command line. If some portions of data are too big for writing in a command line, one or more parameters are file name, so the use adds more complicated data in a file and uses the file for entering data. The interactive input like yours is too inconvenient to the users; one mistake may cause the user to start over, and so on. Try not to get sunken in it.

—SA


我总是发现 scanf 不是在C中读取输入的最佳选择。我建议使用 获取 [ ^ ]然后使用 strtok atoi 进行解析和转换。这是一个更多的工作,但给你(程序员)更好的控制。





使用 fflush(stdin)是一个有用的功能,用于在 scanf 之后刷新输入流,以便删除任何尾随空格和换行符。这显然是我多年来一直缺少的。

[/ edit]
I have always found that scanf is not the best choice for reading input in C. I woud suggest using gets[^] and then using functions such as strtok and atoi to do the parsing and conversion. It is a bit more work but gives you (the programmer) better control.

[edit]
The use of fflush(stdin) is a useful feature to flush the input stream after a scanf, in order to remove any trailing whitespace and newline characters. This is obviously what I have been missing all these years.
[/edit]


执行fflush(stdin)会调用未定义的行为。这是不可能的。你是怎么想出这个想法的呢?



与其他答案相反,我想说使用scanf没有任何问题,只要你确切知道格式字符串中的每个字符正在做什么。特别是,%d做了几件事,第一件事是读取并丢弃输入流中的所有内容,只要isspace()返回true(因此它会跳过所有空格,制表符和换行符)。



这正是输入两个数字然后用



Executing fflush(stdin) invokes undefined behavior. It is out of the question. How did you even come up with the idea that it was a thing?

Contrary to other answers, I would say there is nothing wrong with using scanf, as long as you know exactly what every character in the format string is doing. In particular, %d does several things, the first of which is reading and discarding everything from the input stream as long as isspace() returns true (so it skips all spaces, tabs, and newlines).

That is exactly what makes it natural to enter two numbers and read them with

scanf("%d",&a);
scanf("%d",&b);





如果数字是在两个单独的行上输入的,则第二个scanf读取并丢弃第一个剩余的换行符。如果它们被输入到同一行,用空格或制表符分隔,则第二个scanf读取并丢弃这些空格或制表符。



(我还会查看那些scanfs但是,返回)



If the numbers were entered on two separate lines, the second scanf reads and discards the newline left over by the first one. If they were entered on the same line, separated by spaces or tabs, the second scanf reads and discards those spaces or tabs.

(I would also check what those scanfs return, though)


这篇关于使用fflush(stdin);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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