aboutsummaryrefslogtreecommitdiff
path: root/src/hyper/pages/TEXTFILE.ht
blob: 24fa3611a9d0e1bf65dca1ea4ef7a675980f9193 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
% Copyright The Numerical Algorithms Group Limited 1992-94. All rights reserved.
% !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk.
\newcommand{\TextFileXmpTitle}{TextFile}
\newcommand{\TextFileXmpNumber}{9.81}
%
% =====================================================================
\begin{page}{TextFileXmpPage}{9.81 TextFile}
% =====================================================================
\beginscroll

The domain \spadtype{TextFile} allows \Language{} to read and write
character data and exchange text with other programs.
This type behaves in \Language{} much like a \spadtype{File} of strings,
with additional operations to cause new lines.
We give an example of how to produce an upper case copy of a file.
\xtc{
This is the file from which we read the text.
}{
\spadpaste{f1: TextFile := open("/etc/group", "input")   \bound{f1}}
}
\xtc{
This is the file to which we read the text.
}{
\spadpaste{f2: TextFile := open("/tmp/MOTD", "output")  \bound{f2}}
}
\xtc{
Entire lines are handled using the \spadfunFromX{readLine}{TextFile} and
\spadfunFromX{writeLine}{TextFile} operations.
}{
\spadpaste{l := readLine! f1 \free{f1}\bound{l}}
}
\xtc{
}{
\spadpaste{writeLine!(f2, upperCase l) \free{f2 l}}
}
\xtc{
Use the
\spadfunFrom{endOfFile?}{TextFile} operation to check if you have
reached the end of the file.
}{
\begin{spadsrc}[\free{f1 f2}\bound{Copied}]
while not endOfFile? f1 repeat
    s := readLine! f1
    writeLine!(f2, upperCase s)
\end{spadsrc}
}
\xtc{
The file \spad{f1} is exhausted and should be closed.
}{
\spadpaste{close! f1  \free{Copied}\bound{closed1}}
}

\xtc{
It is sometimes useful to write lines a bit at a time.
The \spadfunFromX{write}{TextFile} operation allows this.
}{
\spadpaste{write!(f2, "-The-")  \free{Copied}\bound{tthhee}}
}
\xtc{
}{
\spadpaste{write!(f2, "-End-")  \free{tthhee}\bound{eenndd}}
}
\xtc{
This ends the line.
This is done in a machine-dependent manner.
}{
\spadpaste{writeLine! f2        \free{eenndd}\bound{LastLine}}
}
\xtc{
}{
\spadpaste{close! f2              \free{LastLine}\bound{closed2}}
}
\noOutputXtc{
Finally, clean up.
}{
\spadpaste{)system rm /tmp/MOTD   \free{closed2}}
}

For more information on related topics,  see
\downlink{`File'}{FileXmpPage}\ignore{File},
\downlink{`KeyedAccessFile'}{KeyedAccessFileXmpPage}\ignore{KeyedAccessFile}, and
\downlink{`Library'}{LibraryXmpPage}\ignore{Library}.
\showBlurb{TextFile}
\endscroll
\autobuttons
\end{page}
%