使用guile 1.8或guile 2,下面的代碼讀取過去的EOF,看起來是多餘的一行,然後停止。這是一個提取的大型程序所產生的效果是看似腐敗了以前讀取的數據。我是否正確使用read-line或測試eof-object?通過EOF的Guile Scheme讀線讀取
(use-modules (ice-9 rdelim))
(define f
(lambda (p)
(let loop ((line (read-line p)))
(format #t "line: ~a\n" line)
(if (not (eof-object? (peek-char p)))
(begin
(let ((m (string-match "^[ \t]*#" line)))
(if m
(begin
(format #t "comment: ~a\n" (match:string m))
(loop (read-line p))
)))
(format #t "read next line\n")
(loop (read-line p)))))))
(define main
(lambda()
(let ((h (open-input-file "test")))
(f h))))
下面是一個最小的樣品虛擬輸入文件:
1
2
3
# comment line
4
5
1
2
3
# comment line
4
5
1
2
3
# comment line
4
5
它需要比渴望體現該問題的幾行。對代碼示例的長度抱歉,但只有在代碼達到這種複雜程度(儘管很小)時纔會出現問題。
的主要問題似乎是事實,你當您發現評論時,每次迭代都會讀取_two lines_,請參閱我的答案以獲取解決方案的不同結構。 –