8.6 I/O Patterns
If you want to process individual lines of a file, then you can use for with in-lines:
| ||||
| ||||
|
If you want to determine whether “hello” appears a file, then you could search separate lines, but it’s even easier to simply apply a regular expression (see Regular Expressions) to the stream:
| ||
> (has-hello? (open-input-string "hello")) | ||
#t | ||
> (has-hello? (open-input-string "goodbye")) | ||
#f |
If you want to copy one port into another, use copy-port from scheme/port, which efficiently transfers large blocks when lots of data is available, but also transfers small blocks immediately if that’s all that is available:
> (define o (open-output-string)) |
> (copy-port (open-input-string "broom") o) |
> (get-output-string o) |
"broom" |