【发布时间】:2012-09-22 06:51:58
【问题描述】:
我有一个程序使用 fseek 来清除我的输入缓冲区,它在 Windows 中运行良好,在 Linux 中 buf 失败。请帮帮我。
#include <stdio.h>
#define NO_USE_FSEEK 0
int main(int argc, char *argv[])
{
char ch = 'a';
int i = 1;
long int fpos = -1;
while(1)
{
printf("loop : %d\n", i);
fseek(stdin, 0L, SEEK_END); /*works in Windows with MinGW, fails in Linux*/
fpos = ftell(stdin);
if (-1 == fpos)
{
perror("ftell failure:"); /*perror tells it is Illegal Seek*/
printf("\n");
}
else
{
printf("positon indicator:%ld\n", fpos);
}
scanf("%c", &ch);
printf("%d : %c\n", (int)ch, ch);
i++;
}
return 0;
}
提前致谢!
【问题讨论】:
-
想象
stdin是一个水龙头。显然你想打开水龙头并“寻找”到最后一滴......然后(通过scanf()电话)再挤出一个滴:)
标签: c linux io inputstream fseek