0

Possible Duplicate:
Using fseek with a file pointer that points to stdin

i have a program that use fseek to clear my input buffer, it works well in Windows, buf fails in Linux. Please help me .

#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;
}

Thanks in advance!

Community
  • 1
  • 1
Tracy
  • 1,988
  • 5
  • 25
  • 36
  • 4
    Imagine `stdin` is a water tap. Apparently you want to turn the tap on and "seek" to the last drop ... and later (with the `scanf()` call) squeeze one more drop out of it :) – pmg Oct 01 '12 at 12:14

3 Answers3

2

This is not the accepted way to "clear your input buffer" on either Windows or Linux.

On windows, using the MSVCRT version of the standard C functions, there is an extension allowing fflush(stdin) for this purpose. Note that on other systems this is undefined behavior.

Linux has a function called fpurge with the same purpose.

However, I have to ask, why do you want to clear your input buffer? If it's the usual complaint people have with scanf not reading to the end of the line, it would be better to write code to actually read and discard the rest of the line (loop with getc until reading a '\n', for example, as in pmg's answer). Clearing the input buffer will tend to skip a large amount of data when used on a redirected file or pipe rather than the normal console/tty input.

Random832
  • 37,415
  • 3
  • 44
  • 63
1

i guess fseek will not work with stdin. Because the size of stdin is not known.

Whoami
  • 13,930
  • 19
  • 84
  • 140
1

Test the return value from fseek() (in fact, test the return value from all <stdio.h> input functions).

if (fseek(stdin, 0, SEEK_END) < 0) { perror("fseek"); exit(EXIT_FAILURE); }

Use the idiom

while ((ch = getchar()) != '\n' && ch != EOF) /* void */;
/* if (ch == EOF)
**     call feof(stdin) or ferror(stdin) if needed; */

to ignore all characters in the input buffer up to the next ENTER (or end of file or input error).

pmg
  • 106,608
  • 13
  • 126
  • 198
  • Why error? It's perfectly valid for input stream to be finished with EOF: (a) when it's redirected from a file, and you read all of it; (b) when user inputs EOF character in the terminal (like when using Ctrl+D in Linux or, AFAIR, Ctrl+Y/Z in Windows cmd). – Piotr Kalinowski Oct 01 '12 at 12:47
  • @PiotrKalinowski: `getchar()` returns EOF in two situations. To identify it the EOF is due to end of file condition or some kind of error you need to call another function (`feof()` or `ferror()`). When I said in my answer "input error" I was including the end of file condition in that group. Now fixed, thanks. – pmg Oct 01 '12 at 13:17