5

每当我在 fgets 之前执行 scanf 时,都会跳过 fgets 指令。我在 C++ 中遇到过这个问题,我记得我必须有一些指令来清除标准输入缓冲区或类似的东西。我想C有一个等价物。它是什么?

谢谢。

4

3 回答 3

5

我敢打赌这是因为 \n 卡在输入流中。

请参阅以下问题之一:

我无法刷新标准输入。
我如何在这里进行法拉盛 STDIN?
scanf() 导致无限循环

这个答案

另外:为什么不使用 scanf()

PSfgets()是一个函数,而不是一个指令。

于 2010-05-25T17:49:00.577 回答
3
于 2010-05-25T17:59:03.770 回答
0

You can just put this line before your fgets() call and that'll get the trailing '\n' generated when you pressed enter

scanf("%c", (char *) stdin);
于 2021-08-28T09:03:37.400 回答