开发者

Prolog: adding text to the end of a file

开发者 https://www.devze.com 2023-03-14 18:42 出处:网络
I want to add text to a database in prolog. Something like adding :- tell(\'a.txt\'), write(\'abc\'), told.

I want to add text to a database in prolog. Something like

adding :- tell('a.txt'), write('abc'), told.

but not overwriting the a.txt. I've tried using append like 开发者_开发问答this:

append('a.txt'),write('abc'), told.

but it didn't work. The listener just give a 'no' response and file is not changed. I'm using Amzi Prolog, btw.

Any help will be appreciated.


You have to use these IO predicates: open/3, write/2, close/1.

adding :- open('a.txt', append, Handle), write(Handle, 'abc'), close(Handle).

Check here


In SWI-Prolog this works:

?- append('a.txt'), write('abc'), told.
true.

?- append('a.txt'), write('abc'), told.
true.

Maybe Amzi Prolog does not have append/1 and this causes failure rather than an exception. Or maybe the file is not writable (and this causes failure, rather than an exception).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号