本文介绍了Prolog如何将文件保存在现有文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

添加新数据后如何保存在现有文件中

How do I save on 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 re-write the text file.

推荐答案

使用open/3和面向流的I/O:

Use open/3 and stream oriented I/O:

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

使用 tell/1told 非常不可靠.很容易发生输出被意外写入另一个文件的情况.

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

这里有一个例子来说明 tell/1told 极其不可靠的属性.

Here is an example to illustrate the extremely unreliable properties of tell/1 and told.

说,你写 tell(file), X >3,写(更大的值),告诉. 只要 X > 就可以了.3.但是对于较小的值,此查询将失败并且没有写入任何内容.那可能是你的意图.但是,程序中其他地方的下一个输出现在将进入 file.这是你永远不想发生的事情.由于这个原因,ISO-Prolog 没有 tell/1told 而是 open/3close/1.

Say, you write tell(file), X > 3, write(biggervalue), told. This works fine as long as X > 3. But with a smaller value this query fails and nothing is written. That might have been your intention. However, the next output somewhere else in your program will now go into the file. That's something you never want to happen. For this reason ISO-Prolog does not have tell/1 and told but rather open/3 and close/1.

这篇关于Prolog如何将文件保存在现有文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-21 02:03
查看更多