如何从文件中读取字节

问题描述 投票:0回答:1

我试图用8.0.3版本的swipl在prolog中读取一个文件到一个字节列表中。

:- use_module(library(readutil)).

try_read_byte(File):-
    open(File, read, Stream),
    get_byte(Stream, B),
    print(B).

try_read_char(File):-
    open(File, read, Stream),
    get_char(Stream, C),
    print(C).

try_read_char 成功,但当我调用 try_read_byte,我得到一个错误。

ERROR: No permission to read bytes from TEXT stream `<stream>(0x56413a06a2b0)'
ERROR: In:
ERROR:    [9] get_byte(<stream>(0x56413a06a2b0),_9686)
ERROR:    [8] try_read_byte("test.pl") at /home/justin/code/decompile/test.pl:5
ERROR:    [7] <user>

从审查源码文档 (https:/www.swi-prolog.orgpldocman?section=error),这似乎是类似类型错误的问题,但我无法根据这一点找出该怎么做。

file stream prolog byte file-read
1个回答
1
投票

要读取二进制文件,你需要指定以下选项 type(binary). 否则 get_byte 提出 permission_error (8.13.1.3节)。这可能会让人感到困惑,因为用户可能在检查正确的权限,而这与问题的实际来源无关。

try_read_byte(File):-
    open(File, read, Stream, [type(binary)])),
    get_byte(Stream, B),
    write(B).

我刚刚用Scryer Prolog试了一下,它按照预期打印了文件的第一个字节。

© www.soinside.com 2019 - 2024. All rights reserved.