为什么ftell(stdin)导致非法查找错误 [英] why ftell( stdin ) causes illegal seek error

查看:165
本文介绍了为什么ftell(stdin)导致非法查找错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码输出非法搜索":

The following code 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) );
}

当我运行cat script | ./a.out以及我刚运行./a.out时,就会发生这种情况.当然,问题出在ftell身上.我的问题是:为什么会发生这种情况?我认为stdin可以寻找. fseek也会导致相同的错误.如果stdin不可搜索,是否可以通过某种方式做同样的事情?

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?

感谢您的答复.

推荐答案

Fifo不可搜索.它们只是缓冲区.一旦从fifo缓冲区read()获得数据,就永远无法检索.

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

然后,标准输入将是一个文件而不是一个 fifo ,因此ftell()将按照您的期望进行操作.

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

这篇关于为什么ftell(stdin)导致非法查找错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆