tags:

views:

45

answers:

2

Hello everyone,

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 :)

A: 

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.
mercator
A: 

Thank you.

But append doesn't work .

I think append is used for lists.

I can not understand, it entered in an infinite loop

write('Enter the animal name'),nl,read(X),repeat,
write('1-mammal   2-birds'),read(Ch),nl,
write('Enter its name'),read(Name),nl,type(Ch,Name).

type(1,Name):-write(mammal(Name)).
type(2,Name):-write(birds(Name)).

Where can I put it?

smile
How does this code relate to the code in your question? Where/how does it go into an infinite loop? Can you provide some more code? Preferably a minimal, complete example. Please edit that into your question, or otherwise this answer if you can... I'll update my answer too...
mercator