2017-05-23 53 views
0

我想弄清楚如何從一個文件中讀取一行與guile方案。從文件中讀取一行

當我問它「讀取端口」或「讀取字符端口」,它成功讀取。

guile -c '(let ((port (open-input-file "foo.txt"))) (display (read port)) (newline) (close-port port))' 

但是,當我要求它讀線時,它失敗。

guile -c '(let ((port (open-input-file "foo.txt"))) (display (read-line port)) (newline) (close-port port))' 

有誰知道我在做什麼錯?我目前在foo.txt所在的目錄中。

回答

1

您的代碼失敗並顯示消息ERROR: Unbound variable: read-line,這意味着readline沒有定義。

必須使用表格(use-modules (ice-9 rdelim))加載函數read-line,然後才能使用它。 (https://www.gnu.org/software/guile/manual/html_node/Input-and-Output.html

這一操作將工作:

guile -c '(use-modules (ice-9 rdelim)) (let ((port (open-input-file "foo.txt"))) 
(display (read-line port)) (newline) (close-port port))'