如果你想处理文件的某一行数据,可以使用for
和in-lines
>(define (upcase-all in)
(for ([l (in-lines in)])
(display (string-upcase l))
(newline)))
>(upcase-all (open-input-string
(string-append
"hello,world!\n"
"Can you hear me, now?")))
如果你想判断是否“hello”出现在一个文件里,你可以使用正则表达式。
>(define (has-hello? in)
(regexp-match? #rx"hello" in))
>(has-hello? (open-input-string "hello"))
#t
>(has-hello? (open-input-string "goodbye"))\
#f
如果你想复制一个端口到另外一个端口,使用racket/port包下面的copy-port。在文件很大时,它将大块传输数据,但是在文件下是,它也能有效传输数据。
>(define o (open-output-string))
>(copy-port (open-input-string "broom") o)
>(get-output-string o)
"broom"