以下代码输出“非法搜索”:
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main() {
errno = 0;
getchar();
getchar();
getchar();
ftell( stdin );
printf( "%s\n", strerror(errno) );
}
当我运行 cat script | 时会发生这种情况./a.out
以及当我刚刚运行 ./a.out
时。当然,问题出在 ftell 上。我的问题是:为什么会发生这种情况?我认为 stdin 是可搜索的。 fseek 也会导致同样的错误。如果 stdin 不可搜索,有什么方法可以做同样的事情吗?
感谢您的回复。
最佳答案
Fifos 不可搜索。它们只是一个缓冲区。一旦数据从 fifo 缓冲区被read()
,就永远无法检索到。
请注意,如果您运行程序:
./a.out < script
那么标准输入将是一个文件,而不是一个fifo,所以ftell()
将执行您期望的操作。
关于c - 为什么 ftell( stdin ) 导致非法查找错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2502489/