关于箭头键和fflush(stdin)的输出

时间:2014-03-19 07:01:49

标签: c output arrow-keys fflush

我们知道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

2 个答案:

答案 0 :(得分:1)

我想你想要fpurgefflush用于输出流,fpurge用于输入流。

答案 1 :(得分: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]

有些编译器定义了刷新输入流的这个功能,但是如果你有一个没有这个特殊增强功能的编译器,那么你将花费数天时间来弄清楚什么是错误的

刷新stdin的解决方案就是这样的

int c;
while ((c = getchar()) != '\n' && c != EOF);
相关问题