Subject: Re: [SWIPL] Problems with streams

From: "Richard A. O'Keefe" <[email-masked]>
Date: Tue, 7 Aug 2001 12:53:02 +1200 (NZST)

Armando Stellato <[email-masked]> wrote:
	open(mario2, write, A, [alias(stream_write)]).
	write(stream_write, pippo).
	open(mario2, read, A, [alias(stream_read)]).
	read(stream_read, Char).
	
	Well, it should give me: Char=pippo

No it most certainly should not.  It should give you an end of file error.

	but it gives me end of file error, why?

Because there IS an end of file error, that's why.

The `read' predicates read a TERM, and if you check the syntax for
terms, the closing full stop ('.' followed by at least one layout
character such as space, tab, or newline) is a REQUIRED part of the syntax.

Had you done

	write(stream_write, pippo),
	write(stream_write, '.\n')

then you would have got the result you expected.

This is one of the reasons why Quintus Prolog included a
portray_clause/1 predicate, which wrote a term out indented as
a clause, and included the terminating full stop.

	I've tried all the possible combinations, all kind of writes, closing
	the write_stream (so to be sure to have debuffered it) and then opening
	the read_stream and one thousand other things....
	why the hell doesn't it go in the right way????
	
Because the built in predicates are doing exactly what they are documented
as doing.
	
write/[1,2] is a *building* block that you can use for building up
a piece of output; to be useful for that, it MUST NOT write a terminating
full stop.

The basic problem is that Prolog uses operator precedence syntax for
data, so some explicit mark is required to end a term.