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 逻辑名)为关闭文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值