writing to a file in prolog 
Author Message
 writing to a file in prolog

hi there. i know i asked this ages ago, but bein a bit of a lazy fool,
i didnt get round to testing it fully. basically, i need to wite stuff
to a file with prolog. when i do:

tell('c:\\bla.txt'), write(anything), told.

all is good. however, i need to wite more than one thing at different
times to this file. if i try doing:

tell('c:\\bla.txt'), write(somethingelse), told.

then what was in the file previously gets over-written. well, that
aint good for me. so, how do i append to that file?

thanks in advance.

Seb



Sat, 18 Jun 2005 03:40:44 GMT  
 writing to a file in prolog

This works under SWI prolog:
open('c:\\foo.txt', append, Stream), tell(Stream), write(somethingelse),
told.

Alternately, you could just write(Stream, somethingelse) directly.

-Benjamin Johnston


Quote:
> hi there. i know i asked this ages ago, but bein a bit of a lazy fool,
> i didnt get round to testing it fully. basically, i need to wite stuff
> to a file with prolog. when i do:

> tell('c:\\bla.txt'), write(anything), told.

> all is good. however, i need to wite more than one thing at different
> times to this file. if i try doing:

> tell('c:\\bla.txt'), write(somethingelse), told.

> then what was in the file previously gets over-written. well, that
> aint good for me. so, how do i append to that file?

> thanks in advance.

> Seb



Sat, 18 Jun 2005 10:41:27 GMT  
 writing to a file in prolog

Quote:

> This works under SWI prolog:
> open('c:\\foo.txt', append, Stream), tell(Stream), write(somethingelse),
> told.

This works, but more or less `by accident'. Many Prologs have two sets of
I/O predicates: the traditional Edinburgh Prolog ones and the ISO ones.  If
both are provided there are generally also some predicates to relate the two
sets.  In SWI-Prolog these are set_output/1, current_output/1 and friends.

If you want to add new data infrequently to a file, you have many options.
First choice is do you want to close the file in between or not and the other
is to use traditional Edinburgh or the ISO predicates.

Closing each time the `correct' code is:

Edinburgh:

append_to_file(File, Term),
        telling(Old),
        append(File),           % is like tell/1 but doesn't truncate
        write(Term),
        told,
        tell(Old).

ISO:

append_to_file(File, Term) :-
        open(Old, append, Stream),
        write(Stream, Term),
        close(Stream).

You can however also keep the file open:

Edinburgh:

append_to_file(File, Term),
        telling(Old),
        tell(File),             % First call in process truncates,
        write(Term),            % other append
        flush_output,           % if you want the data on disk _now_
        tell(Old).

At the end of the program you can close the file explicitely using

        telling(Old),
        tell(File), told,
        tell(Old)

or leave it to Prolog as you quit the Prolog system.

ISO:

At initialisation:

        open(File, write, Stream, [alias(foo)]),

As you want to write:

        write(foo, Term),
        flush_output(foo)

As you terminate

        close(foo).

For flush_output(foo) and close(foo) the same comments as above apply.
You may also choose not to use an alias, and pass the stream-handle
around.  I prefer passing anonymous stream handles, but if you need
them infrequently using aliases makes sense.

I'd go for the ISO predicates.

Quote:
> Alternately, you could just write(Stream, somethingelse) directly.

Yip.

        --- Jan



Sat, 18 Jun 2005 18:43:40 GMT  
 writing to a file in prolog

Ah, thanks for the extra pointers!!!!

-Benjamin Johnston


Quote:


> > This works under SWI prolog:
> > open('c:\\foo.txt', append, Stream), tell(Stream), write(somethingelse),
> > told.

> This works, but more or less `by accident'. Many Prologs have two sets of
> I/O predicates: the traditional Edinburgh Prolog ones and the ISO ones.
If
> both are provided there are generally also some predicates to relate the
two
> sets.  In SWI-Prolog these are set_output/1, current_output/1 and friends.

> If you want to add new data infrequently to a file, you have many options.
> First choice is do you want to close the file in between or not and the
other
> is to use traditional Edinburgh or the ISO predicates.

> Closing each time the `correct' code is:

> Edinburgh:

> append_to_file(File, Term),
> telling(Old),
> append(File), % is like tell/1 but doesn't truncate
> write(Term),
> told,
> tell(Old).

> ISO:

> append_to_file(File, Term) :-
> open(Old, append, Stream),
> write(Stream, Term),
> close(Stream).

> You can however also keep the file open:

> Edinburgh:

> append_to_file(File, Term),
> telling(Old),
> tell(File), % First call in process truncates,
> write(Term), % other append
> flush_output, % if you want the data on disk _now_
> tell(Old).

> At the end of the program you can close the file explicitely using

> telling(Old),
> tell(File), told,
> tell(Old)

> or leave it to Prolog as you quit the Prolog system.

> ISO:

> At initialisation:

> open(File, write, Stream, [alias(foo)]),

> As you want to write:

> write(foo, Term),
> flush_output(foo)

> As you terminate

> close(foo).

> For flush_output(foo) and close(foo) the same comments as above apply.
> You may also choose not to use an alias, and pass the stream-handle
> around.  I prefer passing anonymous stream handles, but if you need
> them infrequently using aliases makes sense.

> I'd go for the ISO predicates.

> > Alternately, you could just write(Stream, somethingelse) directly.

> Yip.

> --- Jan



Mon, 20 Jun 2005 20:01:58 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Writing to a file with ProLog

2. How to write comment in a prolog file?

3. Turbo Prolog: Writing to a file?

4. Turbo Prolog write access to .WKS files

5. Prolog compiler written in Prolog

6. Weird behaviour of file.write() (no data written)?

7. Reading from input file writing to output file

8. Write to 2 file from pipe without using temp file

9. Writing two equal files without creating two files in dct

10. CW4 Writing to a parent file from child file's update form

11. IMAQ Vision - Writing Image Files: File Format

12. How do I replace a file using Write Characters to file.vi

 

 
Powered by phpBB® Forum Software