swi prolog,如何读取txt.file的数据
有人可以帮助我完成文件读取谓词吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定问题出在哪里:读还是写?看来您拥有所需的代码。我重写了它,将内容进一步分开,并提出了这个:
从这里你可以像这样使用它:
如果你想读这个,你可能应该
咨询
它:< code>[marriages] 但您需要将其重命名为以“.pl”结尾。编辑:有关读取文件的更多信息。
在 Prolog 中,有阅读,也有咨询。读取与其他语言一样,您手动从文件中读取数据并对其进行处理。如果您的输入文件也是 Prolog,则可以使用咨询;它相当于解析文件并将其中定义的事实和谓词添加到当前活动的会话中。例如,要查阅
marriage.txt
文件,您可以使用以下命令:现在您已经查阅了该文件,您可以使用数据库中的新事实执行查询:
如果您想获取列表在所有
personal/2
事实中,您可以执行以下操作: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:
From here you can use it like so:
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:Now that you've consulted it, you can perform queries with the new facts in the database:
If you want to get a list of all the
personal/2
facts, you can do something like this: