2011-09-02 40 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