2011-09-02 52 views
1

这是我的示例源代码,我正在使用SWI Prolog,有人可以告诉我如何声明用户键入txt.file的数据。我想将数据保存到txt.file中。SWI Prolog,如何将数据断言为txt文件

start:-display_menu。

display_menu: - 重复, 写( '\ n ======匹配的合作伙伴系统========='),

  write('\n1.Enter user information'), 
      write('\n0.exits'), 
      write('\nEnter your choice:'), 
      read(Choice), 
      selection(Choice), 
      Choice=0. 

选择(1): - get_userinfo 。

选择(0): - !.

get_userinfo:-write( '\ n * 输入用户信息* '),

  write('\nEnter Name:'), 
      read(Name), 
      write('\nEnter Gender:'), 
      read(Gender), 
      write('\nEnter Age:'), 
     read(Age), 
     not(agevalidation(Age)), 
     write('\nEnter the attributes'), 
     get_attribute(Attr), 
     assert(userInfo(Name,Gender,Age,Attr)). 

get_attribute(attr)使用: - 写(' \ n输入' 的高度),

    read(Height), 
        Attr=[Height]. 

年龄验证(年龄): - 年龄< 18, 写('\ n输入有效年龄..')。

+0

请正确格式的代码,并尝试重组“如何断言数据中txt.file用户密钥我想将数据保存到txt.file。”。 AFAIU你想将用户的输入保存到输出.txt文件中? – ierax

回答

1

检查IO predicates;您可能会想要使用open/3和close/3打开/关闭文件,然后写入/ 2。

,如:

open('myfile.txt', write, S), 
write(S,Data), 
close(S). 
+0

我应该在哪里放置“开放”? – fewer