如何将变量的内容写入Rebol 2中的文本文件?

时间:2014-11-07 18:40:13

标签: rebol rebol2

新手问题在这里...... 我想将“what”函数的输出写入文本文件。

所以这就是我所做的:

我创建了一个名为“text”的变量,并为其分配了“what”的输出

text:[what]

现在我想将“text”变量的内容写入txt文件......

感谢任何帮助。提前谢谢!

3 个答案:

答案 0 :(得分:4)

将语句输出写入文件的最简单方法是使用

echo %file.log
what

echo none结束此

>> help echo
USAGE:
      ECHO target 

DESCRIPTION:
     Copies console output to a file.
     ECHO is a function value.

ARGUMENTS:
     target -- (Type: file none logic)

(SPECIAL ATTRIBUTES)
     catch

答案 1 :(得分:3)

不幸的是,what函数没有返回任何值:

在控制台中尝试以下操作:

 print ["Value of `what` is: " what]

所以write %filename.txt [what]无效。

相反,您可以做的是查看what

的来源
source what

返回:

what: func [
    "Prints a list of globally-defined functions."
    /local vals args here total
][
    total: copy []
    vals: second system/words
    foreach word first system/words [
        if any-function? first vals [
            args: first first vals
            if here: find args /local [args: copy/part args here]
            append total reduce [word mold args]
        ]
        vals: next vals
    ]
    foreach [word args] sort/skip total 2 [print [word args]]
    exit
]

看到这个函数只打印(它没有返回它找到的值)我们可以修改脚本来做你想做的事情:

new-what: func [
    "Returns a list of globally-defined functions."
    /local vals args here total collected
][
    collected: copy []
    total: copy []
    vals: second system/words
    foreach word first system/words [
        if any-function? first vals [
            args: first first vals
            if here: find args /local [args: copy/part args here]
            append total reduce [word mold args]
        ]
        vals: next vals
    ]
    foreach [word args] sort/skip total 2 [append collected reduce [word tab args newline]]
    write %filename.txt collected
    exit
]

这个函数有点hackish(设置了文件名,但它会返回你想要的)。您可以扩展函数以接受文件名或执行您想要的任何操作。 tabnewline可以使文件输出更漂亮。

需要注意的重要事项:

  1. 打印返回未设置
  2. 使用source查找功能
  3. write %filename value会一次向文件写出一个值。如果你open一个文件,你可以写更多次。

答案 2 :(得分:0)

相当基本:如果您只想保存一些文字write来恢复它,请使用read;如果您想存储一些数据并使用save来恢复数据,请使用load

>> write %file.txt "Some Text"
>> read %file.txt
== "Some Text"

>> text: [what]
>> save/all %file.r text
>> load %file.r
== [what]

您可以在提示符下获取有关每个字词的更多信息:help save或在线查看:loadsavereadwrite