0

How can I assert a fact into a file without deleting the previous fact?

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

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

But when I use assert or assertz it will do nothing.

Help me please.

Thank you :)

mercator
  • 28,290
  • 8
  • 63
  • 72
smile
  • 59
  • 1
  • 2
  • 5
  • See answer for [Prolog - ASSERT and RETRACT](http://stackoverflow.com/questions/2435237/prolog-assert-and-retract) – ony May 27 '10 at 19:15

1 Answers1

0

tell truncates the file you're writing to.

Use append('animal.txt') instead. That will write to the end of the file.

In reply to your comment:

Where can I put it?

Do you mean the append/1?

Shouldn't the code in your question go in your type/2 definitions (with append replacing tell)? E.g.

type(1, Name) :-
    append('animal.txt'),
    write(mammal(Name)), nl,
    told.
Community
  • 1
  • 1
mercator
  • 28,290
  • 8
  • 63
  • 72