swi prolog,如何读取txt.file的数据

发布于 2024-12-02 21:46:05 字数 383 浏览 0 评论 0原文

有人可以帮助我完成文件读取谓词吗?

get_userinfo: write('\nEnter Name:'),
          readln(Name),
          write('\nEnter Gender:'),
          read(Gender),
          append('marriage.txt') ,
          write(personal(Name,Gender,Age,Attr)),nl,
          told.

这是我的文本文件:

personal(chong,male).
personal(jack,female).
personal(kk,male).

Can someone help me with my file reading predicate?

get_userinfo: write('\nEnter Name:'),
          readln(Name),
          write('\nEnter Gender:'),
          read(Gender),
          append('marriage.txt') ,
          write(personal(Name,Gender,Age,Attr)),nl,
          told.

This is my text file:

personal(chong,male).
personal(jack,female).
personal(kk,male).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国产ˉ祖宗 2024-12-09 21:46:05

不确定问题出在哪里:读还是写?看来您拥有所需的代码。我重写了它,将内容进一步分开,并提出了这个:

get_userinfo(personal(Name, Gender, Age, Attr)) :-
      write('Enter name: '),
      readln([Name|_]),
      write('Enter gender: '),
      readln([Gender|_]),
      write('Enter age: '),
      readln([Age|_]),
      write('Enter attr: '),
      readln([Attr|_]).

record_userinfo(Filename, Person) :-
      append(Filename),
      write(Person), nl,
      told.

从这里你可以像这样使用它:

:- get_userinfo(Person), record_userinfo('marriage.txt', Person).

如果你想读这个,你可能应该咨询它:< code>[marriages] 但您需要将其重命名为以“.pl”结尾。

编辑:有关读取文件的更多信息。

在 Prolog 中,有阅读,也有咨询。读取与其他语言一样,您手动从文件中读取数据并对其进行处理。如果您的输入文件也是 Prolog,则可以使用咨询;它相当于解析文件并将其中定义的事实和谓词添加到当前活动的会话中。例如,要查阅 marriage.txt 文件,您可以使用以下命令:

:- ['marriage.txt'].

现在您已经查阅了该文件,您可以使用数据库中的新事实执行查询:

?- ['marriages.txt'].
% marriages.txt compiled 0.00 sec, 1,328 bytes
true.

?- personal(X, Gender).
X = chong,
Gender = male ;
X = jack,
Gender = female ;
X = kk,
Gender = male.

?- personal(X, male).
X = chong ;
X = kk.

?- personal(chong, X).
X = male.

如果您想获取列表在所有 personal/2 事实中,您可以执行以下操作:

?- setof(personal(X,Y), personal(X,Y), Qs).
Qs = [personal(chong, male), personal(jack, female), personal(kk, male)].

Not sure where the problem is: reading or writing? It looks like you have the code you need. I rewrote it to separate things out a little more and came up with this:

get_userinfo(personal(Name, Gender, Age, Attr)) :-
      write('Enter name: '),
      readln([Name|_]),
      write('Enter gender: '),
      readln([Gender|_]),
      write('Enter age: '),
      readln([Age|_]),
      write('Enter attr: '),
      readln([Attr|_]).

record_userinfo(Filename, Person) :-
      append(Filename),
      write(Person), nl,
      told.

From here you can use it like so:

:- get_userinfo(Person), record_userinfo('marriage.txt', Person).

If you're trying to read this in, you should probably just consult it: [marriages] but you'll need to rename it to end in '.pl'.

Edit: more information about reading files.

In Prolog, there's reading and there's consulting. Reading is as in other languages, you manually read in data from the file and process it. Consulting is something you can use in cases where your input file is also Prolog; it amounts to parsing the file and adding the facts and predicates defined in it to the currently active session. For example, to consult the marriage.txt file, you would use this:

:- ['marriage.txt'].

Now that you've consulted it, you can perform queries with the new facts in the database:

?- ['marriages.txt'].
% marriages.txt compiled 0.00 sec, 1,328 bytes
true.

?- personal(X, Gender).
X = chong,
Gender = male ;
X = jack,
Gender = female ;
X = kk,
Gender = male.

?- personal(X, male).
X = chong ;
X = kk.

?- personal(chong, X).
X = male.

If you want to get a list of all the personal/2 facts, you can do something like this:

?- setof(personal(X,Y), personal(X,Y), Qs).
Qs = [personal(chong, male), personal(jack, female), personal(kk, male)].
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文