Search code examples
filestreamprologbytefile-read

How to read bytes from a file


I'm attempting to read a file into a list of bytes in prolog, using swipl version 8.0.3.

:- 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 succeeds, but when I call try_read_byte, I get an error:

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>

From reviewing the source code/documentation (https://www.swi-prolog.org/pldoc/man?section=error), it seems as if this is something like a type error, but I'm not able to figure out what to do based on that.


Solution

  • To read binary, you need to specify the option type(binary). Otherwise get_byte raises a permission_error as specified by the standard (section 8.13.1.3). This can be confusing as the user might be checking for correct permissions, which has nothing to do with the actual source of the problem.

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

    I just tried this with Scryer Prolog, and it prints the first byte of the file as expected.