简体   繁体   中英

Prolog how to save file in an existing file

How do I save on an existing file after adding new data

add_a_link(X,Y) :-
    tell('alink.txt'),
    write(X),
    write('.'),
    write(Y),
    write('.'),
    put(10),
    told,
    write('data written'),
    nl.

this code only re-write the text file.

Use open/3 and stream oriented I/O:

open(file, append, S), write(S, info(X,Y)), put_char(S,.), nl(S), close(S).

Using tell/1 and told is extremely unreliable. It easily happens that the output is written to another file accidentally.

Edit: Here is an example to illustrate the extremely unreliable properties of tell/1 and told .

Say, you write tell(file), X > 3, write(biggervalue), told. This works fine as long as X > 3 . But with a smaller value this query fails and nothing is written. That might have been your intention. However, the next output somewhere else in your program will now go into the file . That's something you never want to happen. For this reason ISO-Prolog does not have tell/1 and told but rather open/3 and close/1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM