使用guile 1.8或guile 2,下面的代码读取过去的EOF,看似多了几行,然后停止。这在提取的较大程序中的效果似乎是破坏了先前读取的数据。我是否错误地使用了读取线或测试eof-object?
(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
要显示问题需要多于几行。请注意代码示例的长度,但只有在代码达到这种复杂程度时才会出现问题(尽管很小)。
答案 0 :(得分:1)
我建议重写该过程,它似乎不是正确的读取文件并循环其行的方法。请尝试这个:
(define (f)
(let loop ((line (read-line)))
(if (not (eof-object? line))
(begin
(format #t "line: ~a\n" line)
(let ((m (string-match "^[ \t]*#" line)))
(if m (format #t "comment: ~a\n" line)))
(format #t "read next line\n")
(loop (read-line))))))
(define (main)
(with-input-from-file "test" f))
使用您的示例输入,调用(main)
在控制台上输出以下输出,这有望达到预期目的:
line: 1
read next line
line: 2
read next line
line: 3
read next line
line: # comment line
comment: # comment line
read next line
line: 4
read next line
line: 5
read next line
line: 1
read next line
line: 2
read next line
line: 3
read next line
line: # comment line
comment: # comment line
read next line
line: 4
read next line
line: 5
read next line
line: 1
read next line
line: 2
read next line
line: 3
read next line
line: # comment line
comment: # comment line
read next line
line: 4
read next line
line: 5
read next line