Skip to content

Commit 74fb5fc

Browse files
committed
file read
1 parent a2f01df commit 74fb5fc

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

ty-scheme/ch07/file-ports.scm

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
(define out
2+
(lambda (what)
3+
(begin
4+
(display what)
5+
(newline))))
6+
7+
(newline)
8+
9+
; the s-expr below is identical
10+
; current-output-port is used if no port is given
11+
12+
(display 9)
13+
(display 9 (current-output-port))
14+
; 99
15+
16+
(newline)
17+
18+
; now we've known 'open-{input,output}-file' & 'close-{input,output}-port'
19+
; let's try to play with them, and do some IO operations
20+
21+
(define infile (open-input-file "in.txt"))
22+
23+
(out (read-char infile))
24+
; #\T
25+
26+
(out (read-line infile))
27+
; "each"
28+
29+
; write a function to read all the rest content!
30+
(define read-all
31+
(lambda (port)
32+
; we keep a list for what we've read
33+
(let read-next (
34+
(cur-content '()))
35+
(define cur-ch (read-char port))
36+
(if (eof-object? cur-ch)
37+
(list->string (reverse cur-content))
38+
(read-next (cons cur-ch cur-content))))))
39+
(out (read-all infile))
40+
(close-input-port infile)

ty-scheme/ch07/in.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Teach
2+
Yourself
3+
Scheme
4+
in
5+
Fixnum
6+
Days
7+
Nice boat!

0 commit comments

Comments
 (0)