#include #include

Ftell (stdin) causes an illegal search error

The following code displays "Illegal Search":

#include <stdio.h> #include <errno.h> #include <string.h> int main() { errno = 0; getchar(); getchar(); getchar(); ftell( stdin ); printf( "%s\n", strerror(errno) ); } 

This happens when I run "cat script | ./a.out" and also when I just run "./a.out". Of course, the problem is with ftell. My question is: why is this happening? I think stdin can be searched. fseek also causes the same error. If stdin is not searchable, is there a way to do the same?

Thank you for your responses.

+6
c file-io ftell
source share
1 answer

Fifos are not searchable. It is just a buffer. After the data was read() from the fifo buffer, it can never be recovered.

Please note that if you run your program:

 ./a.out < script 

then standard input will be a file, not fifo, so ftell() will do what you expect.

+12
source share

All Articles