如何在没有方案中的引号的情况下将字符串写入文件

时间:2015-12-10 19:27:05

标签: scheme chicken-scheme

我正在尝试将字符串写入文件,但每次我这样做都会引用它。

我试过

(call-with-output-file file-path
  (lambda(output-port)(write "some text" output-port)))

(let ((p (open-output-file file-path)))
      (write "some text" p)
      (close-output-port p))

但在这两种情况下我都期望"some text"但得到"\"some text\""

我目前正在从事鸡计划,但我认为这不重要。

1 个答案:

答案 0 :(得分:4)

write用于将S表达式序列化为文件。它与read相反,它将序列化的S表达式读回列表,符号,字符串等。这意味着write将输出与源代码中相同的所有内容。

如果您只想将字符串输出到端口,请使用display

(call-with-output-file file-path
  (lambda(output-port)
    (display "some text" output-port)))

或者在CHICKEN中,您可以使用printffprintf

(call-with-output-file file-path
  (lambda(output-port)
    (fprintf output-port 
             "Printing as s-expression: ~S, as plain string: ~A"
             "some text"
             "some other test")))

这会将以下内容打印到文件中:

Printing as s-expression: "some text", as plain string: some other text