Prolog, how to save a file in an existing file

How to save 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 overwrites the text file.

+4
source share
1 answer

Use open/3 and stream input / output reference:

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

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

Edit: Here is an example illustrating the highly unreliable properties of tell/1 and told .

Let's say you write tell(file), X > 3, write(biggervalue), told. This works fine until X > 3 . But with a lower value, this request fails and nothing is written. Perhaps this was your intention. However, the next output somewhere else in your program will now go to file . This is something you will never want. For this reason, ISO-Prolog does not have tell/1 and told , but rather open/3 and close/1 .

+3
source

Source: https://habr.com/ru/post/902505/


All Articles