CLIPS-读写关闭文件

一、从标准设备读

1、使用(bind ?name (read))

2、操作

CLIPS> (clear)
CLIPS> (defrule getname
=>
(printout t "what's you name?")
(bind ?name (read))
(printout t "you are "?name crlf)
(assert (user ?name)))
CLIPS> (reset)
CLIPS> (run)
what's you name?zhanghua
you are zhanghua
CLIPS> (facts)
f-0(initial-fact)
f-1(user zhanghua)
For a total of 2 facts.
CLIPS>

二、文件操作

1、CLP文件

(defrule MAIN::getname
=>
(printout t "what's youname?")
(bind ?name (read))
(printout t "you are " ?namecrlf)
(assert (user ?name)))

(defrule MAIN::savename
?user <- (user?name)
=>
(retract ?user)
(open "E:users.dat" userfile"w")
(printout userfile ?namecrlf)
(close userfile))

(defrule MAIN::printname
?print<-(print-user print-user)
=>
(retract ?print)
(open "E:\users.dat" userfile"r")
(bind ?name (readuserfile))
(printout t "you are " ?namecrlf)
(close userfile))

2、操作

CLIPS> (clear)
CLIPS> (reset)
CLIPS> (load "E:\jjj.clp")
***
TRUE
CLIPS> (run)
what's you name?liuhui
you are liuhui
CLIPS> (assert (print-user print-user))
<Fact-2>
CLIPS> (facts)
f-0(initial-fact)
f-2(print-user print-user)
For a total of 2 facts.
CLIPS> (run)
you are liuhui
CLIPS>

3、上例完成从键盘中输入一个用户名,然后存入文件,打印时从文件调出

4、(open 文件名 逻辑名 "w")为打开文件,并以写操作方式打开,"w"改为"r"为读操作

  (close 逻辑名)为关闭文件

你可能感兴趣的:(F#)