在标准输入上使用really_input_string进行非法寻道

玩技站长 科技常识评论64字数 872阅读模式
摘要最佳答案:我正在重定向一些代码来接受来自stdin的输入(除了文件),当重定向stdin时,Unix查找操作只对普通文件有意义,即存储在磁盘(或类似的随机可寻址介质)上的文件,终端...
提问开始:

我正在翻新一些代码以接受来自stdin的输入(除了文件)。文章源自玩技e族-https://www.playezu.com/748324.html

print_string (really_input_string stdin (in_channel_length stdin))

当我重定向stdin时,这是有效的:-文章源自玩技e族-https://www.playezu.com/748324.html

$ ./a.out < /tmp/lorem.txt
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod 

但是在没有等待我输入的情况下失败了:文章源自玩技e族-https://www.playezu.com/748324.html

$ ./a.out
Fatal error: exception Sys_error("Illegal seek")
$

或:文章源自玩技e族-https://www.playezu.com/748324.html

$ cat /tmp/lorem.txt | ./a.out
Fatal error: exception Sys_error("Illegal seek")

我怎样才能让后者也工作呢?文章源自玩技e族-https://www.playezu.com/748324.html

回答开始:得票数 1文章源自玩技e族-https://www.playezu.com/748324.html

你没有提到你使用的是什么系统。文章源自玩技e族-https://www.playezu.com/748324.html

Unix查找操作仅对常规文件有意义,即存储在磁盘(或类似的随机可寻址介质)上的文件。在通常的Unix实现中,终端设备或管道上的查找会被忽略。但是,在您正在使用的系统中,这些似乎被视为错误。这让我怀疑您使用的不是类Unix(或完全类Unix)系统。文章源自玩技e族-https://www.playezu.com/748324.html

无论如何,问题似乎是in_channel_length会查找到文件的末尾,以确定它有多大。在您的系统中,当输入来自终端或管道时,这不起作用。文章源自玩技e族-https://www.playezu.com/748324.html

即使在Unix系统上,当输入来自管道或终端时,也很难看到代码如何按预期工作。文章源自玩技e族-https://www.playezu.com/748324.html

我建议你编写自己的循环来阅读,直到你看到EOF。

下面是一个粗略的实现,对于一个文本文件来说可能已经足够好了:

let my_really_read_string in_chan =
    let res = Buffer.create 1024 in
    let rec loop () =
        match input_line in_chan with
        | line ->
            Buffer.add_string res line;
            Buffer.add_string res "n";
            loop ()
        | exception End_of_file -> Buffer.contents res
    in
    loop ()
玩技站长微信
添加好友自动发送入群邀请
weinxin
rainbow-shownow
玩技官方公众号
官方微信公众号
weinxin
PLAYEZU
 
  • 版权提示:本站仅供存储任何法律责任由作者承担▷诈骗举报◁▷新闻不符◁▷我要投稿◁
    风险通知:非原创文章均为网络投稿真实性无法判断,侵权联系2523030730
    免责声明:内容来自用户上传发布或新闻客户端自媒体,切勿!切勿!切勿!添加联系方式以免受骗。
  • 原创转载:https://www.playezu.com/748324.html
    转载说明: 点我前往阅读>>>
匿名

发表评论

匿名网友
确定