Reputation: 17955
I want to write a program in Prolog which processes binary data. It shall operate in a UNIX fashion, i.e. if no arguments were given, it should read from stdin
/ user_input
and write to stdout
/ user_output
to act as filter in pipes.
As long as my data is textual, everything is running fine, but as soon as my data is binary, the output is weird and completely not what I expected, sometimes I get strange messages like Illegal multibyte Sequence
. The same happens with streams which I opened using open/3
.
Upvotes: 1
Views: 282
Reputation: 17955
Per default, user_input
and user_output
are set to type(text)
and encoding(X)
with X
being the encoding of the platform derived from the environment. If you want to process binary data, you need to change the type of the stream to type(binary)
or the encoding of the stream to encoding(octet)
. You can do so by using set_stream/2
, like this:
:- set_stream(user_input, type(binary)).
:- set_stream(user_output, type(binary)).
or
:- set_stream(user_input, encoding(octet)).
:- set_stream(user_output, encoding(octet)).
For streams which you open yourself, you can also use open/4
instead, using i.e. open(Filename, read, StreamIn,
[type(binary)]
)
or open(Filename, read, StreamIn,
[encoding(octet)]
)
.
When what you're reading is really binary data, type(binary)
is the preferred method.
Upvotes: 2