【发布时间】:2018-03-19 05:11:20
【问题描述】:
我正在使用这个函数读取文件并将它们存储为字符串:
(defun file-to-str (path)
(with-open-file (stream path) :external-format 'utf-8
(let ((data (make-string (file-length stream))))
(read-sequence data stream)
data)))
如果文件只有 ASCII 字符,我会按预期得到文件的内容;但是如果有超过 127 的字符,我会在字符串的末尾为每个超过 127 的字符得到一个空字符 (^@)。所以,在$ echo "~a^?" > ~/teste 之后,我得到了
CL-USER> (file-to-string "~/teste")
"~a^?
"
;但是在 echo "aaa§§§" > ~/teste 之后,REPL 给了我
CL-USER> (file-to-string "~/teste")
"aaa§§§
^@^@^@"
等等。我怎样才能解决这个问题?我在 utf-8 语言环境中使用 SBCL 1.4.0。
【问题讨论】:
标签: file-io lisp common-lisp unicode-string