Using 'tell' with multiple files

I’m using: SWI-Prolog version 7.6.4, 64 bit, threaded

I want the code to:
Open two files (AAA and BBB) and write to them sequentially.

But what I’m getting is:
File BBB is not created

My code looks like this:

go :- tell('aaa'),
    write('First line of AAA'),nl,
    tell('bbb'),
    write('First line of BBB'),nl,
    tell(user),
    write('This goes on the screen'),nl,
    tell('aaa'),
    write('Second line of AAA'),nl,
    told.

This code is an example from Michael A Covington’s Prolog Programming in Depth with copyright date 1997. I assume that this functionality is not available in SWI-Prolog but can’t seem to find the answer by searching the web. I am a newbie to Prolog.

Hmmm … looks like a bug.

You can do what you want the more modern way:

    open(aaa, write, AAA),
    open(bbb, write, BBB),
    format(AAA, 'First line of AAA~n', []),
    format(BBB, 'First line of BBB~n', []),
    format(user, 'This goes on the screen~n', []),
    format(AAA, 'Second line of AAA~n', []),
    close(AAA),
    close(BBB).

Also, write('...'), nl can be written more compactly as writeln('...').

1 Like

No bug :slight_smile: The stream associated with bbb is never flushed. After quitting Prolog it flushes and closes all streams and bbb is filled. You could add a told/0 call after writing to bbb.

But, as @peter.ludemann says, tell/1 and friends are old pre-standard predicates. I’ve tried removing them when adding support for ISO open/3 and friends, but go so many complains that I re-added them :frowning:

3 Likes

Peter, Jan,
Thank you for your help. As a Prolog newbie it’s really nice for two such eminent people to respond to my questions. I’ll do some research to learn the more modern methods of writing to files.

Ron

The heart is for having them added again :blush:

2 Likes