The following c开发者_运维知识库ode outputs "Illegal seek":
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main() {
errno = 0;
getchar();
getchar();
getchar();
ftell( stdin );
printf( "%s\n", strerror(errno) );
}
This occurs when I run cat script | ./a.out
as well as when I just run ./a.out
. The problem is with ftell, of course. My question is: why does this occur? I would think stdin can be seekable. fseek also causes the same error. If stdin is not seekable, is there some way I can do the same sort of thing?
Thank you for your replies.
Fifos aren't seekable. They are simply a buffer. Once data has been read()
from a fifo buffer, it can never be retrieved.
Note that if you ran your program:
./a.out < script
then standard input would be a file and not a fifo, so ftell()
will then do what you expect.
精彩评论