简体   繁体   中英

SWI Prolog , how to assert data to txt file

This is my sample source code , I am using SWI Prolog , can someone tell me how to assert data that user key in to txt.file. I want save the data in to txt.file.

start  :-display_menu.

display_menu:- repeat,
               write('\n======Matching Partner System========='),
            write('\n1.Enter user information'),
            write('\n0.exits'),
            write('\nEnter your choice:'),
            read(Choice),
            selection(Choice),
            Choice=0.



selection(1):-get_userinfo.


selection(0):-!.

get_userinfo:-write('\n***Enter User Information***'),
          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):- write('\nEnter the height'),
                      read(Height),
                      Attr=[Height].

agevalidation(Age):-Age<18,
                    write('\nEnter valid age..').

check the IO predicates ; you will probably want to use open/3 and close/3 to open/close a file and then write/2.

like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM