Transput
The Revised Report models I/O with three notions:
34.1 Concepts: books, channels and files
The Revised Report models I/O with three notions:
A book — the stored data (a file on disk, the terminal), organized in pages, lines and characters.
A channel — a kind of device with properties (can it be read? written? repositioned?): stand in channel, stand out channel, stand back channel, and implementation-specific ones.
A file (mode FILE) — the connection between a program and a book via a channel, holding the current position and event procedures.
Standard files: stand in, stand out, stand back (and, in a68g, stand error).
34.2 Unformatted transput
Layout routines: newline, newpage, space, backspace. Conversion routines: whole, fixed, float.
print((x, y, newline)); # = put(stand out, (x, y, newline)) #
read((n, name)); # = get(stand in, (n, name)) #
put(f, ("total: ", whole(t, 0), newline));
get(f, (line, newline)); # read the rest of a line into a STRING, then skip the newline #34.3 Formatted transput
Formats are written between dollar signs and used with printf, readf, putf, getf:
digit, leading zero shown as space exponent marker (e.g. d.3de+2d) sign frame (always / only when negative) g, g(w), g(w, d) general pattern (like whole/fixed) n before a pattern, e.g. 3d, 5x dynamic replicator computed from expression e grouping for replication
Spaces inside formats are ignored, so you can lay patterns out readably.
BEGIN
INT n = 42;
REAL x = 3.14159;
printf(($"n = "g(0)l$, n)); # g(0): general pattern, minimal width #
printf(($"padded: "5zdl$, n)); # z: zero-suppressed digit, d: digit #
printf(($"real: "zd.3dl$, x)); # 3.142 #
printf(($"two values: "g(0)", "g(0, 2)l$, n, x));
printf(($3(4d)l$, 1, 22, 333)); # replicated pattern: 000100220333 #
FORMAT money = $"EUR "zzzd.2dl$; # a named FORMAT value #
printf((money, 1234.5))
END34.4 Files
open returns 0 on success. In a68g, opening a non-existent file on stand out channel creates it; the Report also provides establish (create a new book with given size) and create (a scratch file).
on logical file end installs an event routine called when reading past the end; here it jumps out of the loop.
BEGIN
FILE output;
IF open(output, "squares.txt", stand out channel) /= 0 THEN
print(("cannot open squares.txt", newline)); stop
FI;
FOR i TO 10 DO put(output, (whole(i, 0), " ", whole(i * i, 0), newline)) OD;
close(output);
FILE input;
IF open(input, "squares.txt", stand in channel) /= 0 THEN
print(("cannot read squares.txt", newline)); stop
FI;
INT total := 0;
on logical file end(input, (REF FILE f) BOOL: GOTO finished);
DO
INT a, b;
get(input, (a, b, newline));
total +:= b
OD;
finished:
close(input);
print(("sum of squares from file = ", whole(total, 0), newline))
END34.5 Reading line by line
BEGIN
FILE input;
IF open(input, "input.txt", stand in channel) /= 0 THEN print("no file"); stop FI;
INT lines := 0;
on logical file end(input, (REF FILE f) BOOL: GOTO eof);
DO
STRING line;
get(input, (line, newline));
lines +:= 1;
print((whole(lines, 4), ": ", line, newline))
OD;
eof:
close(input)
END34.6 String-based transput and conversions
Convert numbers to strings with whole, fixed, float (they *return* STRINGs).
a68g supports associate to connect a FILE to a STRING so get/put operate on in-memory text; see the a68g manual.