以下是家庭作业/考试准备的一部分:我正在尝试编写一些简明的代码来从文本文件中读取行。空行应包括在内。以下是我想出的代码:
(defun read-file (filename)
(do* ((streamin (open filename)) ;open the file
content
(line (read-line streamin nil 'eof) ;read from file in loop
(read-line streamin nil 'eof)))
((equal line 'eof) (close streamin) ;close the stream if eof is reached
(reverse content))
(setq content (cons line content)))) ;add a read line to the returned symbol
Run Code Online (Sandbox Code Playgroud)
在Sublime Texteditor(或记事本)中创建的文本文件的预期输出,如下所示:
this is
a
test
file
Run Code Online (Sandbox Code Playgroud)
这是:
("this is" "" "a" "" "test" "file")
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下信息:
("this is^M" "^M" "a^M" "^M" "test^M" "file^M")
Run Code Online (Sandbox Code Playgroud)
“ ^ M”来自哪里?它们是通过记事本插入的吗?还是因为我的代码而在那里?他们到底是什么?我猜他们表明一个新的线。
如果将它们插入到高处,我必须用什么替代方法来产生预期的输出?记事本也一样。练习的下一步是使用过滤器功能从输出中删除空行,我想这可以通过将等于空行的行进行比较来实现。但是,当插入这些^ …