State fact to file in prolog

How can I state a fact in a file without deleting the previous fact?

In the next line, when I execute it twice, the second fact overwrites the first fact:

tell('animal.txt'),write(Animal),nl,told.

But when I use assertor assertz, it won’t do anything.

Help me please.

Thank:)

0
source share
1 answer

tell truncates the file you are writing.

Use append('animal.txt'). This will be written to the end of the file.

In response to your comment :

Where can I put it?

Do you mean append/1?

Shouldn't the code be entered in your question in the definitions type/2(with the addition of the tell substitution)? For instance.

type(1, Name) :-
    append('animal.txt'),
    write(mammal(Name)), nl,
    told.
0
source

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


All Articles