0

正如我们所知,arrow keys产生两个输出,即224 and (72 or 80 or 75 or 77)

代码 1:-

char ch,ch1;

ch=getch();

ch1=getch();

printf("%c \n %c",ch,ch1);

在上述情况下,我输入一个arrow key然后224存储在ch,相应的输出存储在ch1

代码 2:-

char ch,ch1;

ch=getch();

fflush(stdin);

ch1=getch();

printf("%c\n%c",ch,ch1);

同样的事情也发生在代码 2 中。
所以我想知道为什么fflush(stdin)不将相应的输出刷新到224.

4

2 回答 2

1

I think you want fpurge. fflush is for output streams, fpurge is for input streams.

于 2014-03-19T07:12:25.890 回答
1

fflush(stdin) 虽然适用于某些实现,但它仍然是未定义的行为。根据标准fflush, fflush 仅适用于输出/更新流。

int fflush(FILE *ostream);
If stream points to an output stream or an update stream in which the most recent operation was not input, fflush() shall cause any unwritten data for that stream to be written to the file, [CX] [Option Start]  and the last data modification and last file status change timestamps of the underlying file shall be marked for update. [Option End]

一些编译器已经定义了刷新输入流的功能,但是如果你的编译器没有这种特殊的增强功能,那么你将花费数天时间试图找出问题所在

刷新标准输入的解决方案是这样的

int c;
while ((c = getchar()) != '\n' && c != EOF);
于 2014-03-19T07:49:20.920 回答