Debian Codegraph @

docsrc/xlisp/xlisp-doc/tutorials/file-io.htm

raw
1 <html><head><title>XLISP: An Object-oriented Lisp</title></head>
2
3 <style type="text/css">
4 .example {
5 color: #000000;
6 background-color: #F5F5F5;
7 padding: 8px;
8 border: #808080;
9 border-style: solid;
10 border-width: 1px;
11 width:auto;
12 }
13 .button {
14 color: #000000;
15 background-color: #F5F5F5;
16 padding-top: 1px;
17 padding-bottom: 1px;
18 padding-left: 4px;
19 padding-right: 8px;
20 border: #808080;
21 border-style: solid;
22 border-width: 1px;
23 white-space: pre;
24 }
25 .box {
26 color: #000000;
27 padding-top: 4px;
28 padding-bottom: 4px;
29 padding-left: 16px;
30 padding-right: 16px;
31 border: #808080;
32 border-style: solid;
33 border-width: 1px;
34 }
35 </style>
36
37 <body>
38
39 <a href="../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
40 <a href="../manual/contents.htm">Contents</a> |
41 <a href="tutorials.htm">Tutorials</a> |
42 <a href="../examples/examples.htm">Examples</a> |
43 <a href="../reference/reference-index.htm">Reference</a>
44
45 <hr>
46
47 <h1>File I/O</h1>
48
49 <hr>
50
51 <ol>
52 <li><nobr><a href="#1">File I/O Examples</a> - from the XLISP 2.0 manual, by David Betz</nobr></li>
53 <ul>
54 <li><nobr><a href="#1-1">Input from a File</a></nobr></li>
55 <li><nobr><a href="#1-2">Output to a File</a></nobr></li>
56 <li><nobr><a href="#1-3">A Slightly More Complicated File Example</a></nobr></li>
57 <li><nobr><a href="#1-4">Closing Files via 'unwind-protect'</a></nobr></li>
58 </ul>
59 <li><nobr><a href="#2">Files and Directories</a></nobr></li>
60 <ul>
61 <li><nobr><a href="#2-1">setdir</a></nobr></li>
62 <li><nobr><a href="#2-2">listdir</a></nobr></li>
63 </ul>
64 <li><nobr><a href="#3">Testing Existence</a></nobr></li>
65 <ul>
66 <li><nobr><a href="#directory-exists-p">directory-exists-p</a></nobr></li>
67 <li><nobr><a href="#file-exists-p">file-exists-p</a></nobr></li>
68 <li><nobr><a href="#file-or-directory-exists-p">file-or-directory-exists-p</a></nobr></li>
69 </ul>
70 <li><nobr><a href="#4">Text File I/O</a></nobr></li>
71 <ul>
72 <li><nobr><a href="#">Reading from a Text File</a></nobr></li>
73 <li><nobr><a href="#">Writing to a Text File</a></nobr></li>
74 <li><nobr><a href="#">Appending to a Text File</a></nobr></li>
75 </ul>
76 <li><nobr><a href="#5">Binary File I/O</a></nobr></li>
77 </ol>
78
79 <a name="1"></a>
80
81 <hr>
82
83 <h2>1&nbsp; File I/O Examples</h2>
84
85 <a name="1-1"></a>
86
87 <hr>
88
89 <h2>1.1&nbsp; Input from a File</h2>
90
91 <hr>
92
93 <p>The basics of file i/o with XLISP:</p>
94
95 <ul>
96
97 <li><p>To open a file for input, use the
98 <a href="../reference/open.htm">open</a> function with the keyword argument
99 :direction set to :input.</p></li>
100
101 <li><p>To open a file for
102 output, use the <a href="../reference/open.htm">open</a> function with the
103 keyword argument :direction set to :output.</p></li>
104
105 <li><p>To close a file, use the <a href="../reference/close.htm">close</a>
106 function.</p></li>
107
108 </ul>
109
110 <p>The <a href="../reference/open.htm">open</a> function takes a single
111 required argument which is the name of the file to be opened. This name can
112 be in the form of a string or a symbol. <nobr>The
113 <a href="../reference/open.htm">open</a></nobr> function returns an object
114 of type '<nobr>file-stream</nobr>' if it succeeds in opening the specified
115 file. <nobr>It returns</nobr> the value
116 <a href="../reference/nil.htm">NIL</a> if it fails.</p>
117
118 <p>In order to manipulate the file, it is necessary to save the value
119 returned by the <a href="../reference/open.htm">open</a> function.
120 This is usually done by assigning it to a variable with the
121 <a href="../reference/setq.htm">setq</a> special form or by binding it using
122 <a href="../reference/let.htm">let</a> or
123 <a href="../reference/let-star.htm">let*</a>. Here is an example:</a>
124
125 <pre class="example">
126 (setq file-stream (open "init.lsp" :direction :input))
127 </pre>
128
129 <p>Evaluating this expression will result in the file 'init.lsp' being
130 opened. The file object that will be returned by the
131 <a href="../reference/open.htm">open</a> function will be
132 assigned to the variable '<nobr>file-stream</nobr>'.</p>
133
134 <p>It is now possible to use the file for input. <nobr>To read</nobr> an
135 expression from the file, just supply the value of the
136 '<nobr>file-stream</nobr>' variable as the optional 'stream' argument to the
137 <a href="../reference/read.htm">read</a> function:</p>
138
139 <pre class="example">
140 (read file-stream)
141 </pre>
142
143 <p>Evaluating this expression will result in reading the first expression
144 from the file 'init.lsp'. The expression will be returned as the
145 result of the <a href="../reference/read.htm">read</a> function.
146 More expressions can be read from the file using further calls to the
147 <a href="../reference/read.htm">read</a> function. When there
148 are no more expressions to read, the
149 <a href="../reference/read.htm">read</a> function will return
150 <a href="../reference/nil.htm">NIL</a> [or whatever value was
151 supplied as the second argument to
152 <a href="../reference/read.htm">read</a>].</p>
153
154 <p>Once you are done reading from the file, you should close it. To close
155 the file, use the <a href="../reference/close.htm">close</a>
156 function:</p>
157
158 <pre class="example">
159 (close file-stream)
160 </pre>
161
162 <p>Evaluating this expression will cause the file to be closed.</p>
163
164 <p>&nbsp;&nbsp;<a href="#top">Back to Top</a></p>
165
166 <a name="1-2"></a>
167
168 <hr>
169
170 <h2>1.2&nbsp; Output to a File</h2>
171
172 <hr>
173
174 <p>Writing to a file is pretty much the same as reading from one. You need
175 to open the file first. This time you should use the
176 <a href="../reference/open.htm">open</a> function to indicate
177 that you will do output to the file:</p>
178
179 <pre class="example">
180 (setq file-stream (open "test.dat" :direction :output))
181 </pre>
182
183 <p>Evaluating this expression will open the file 'test.dat' for output.
184 <nobr>If the</nobr> file already exists, its current contents will be
185 discarded. <nobr>If it</nobr> doesn't already exist, it will be created.
186 <nobr>In any</nobr> case, a '<nobr>file-stream</nobr>' object will be
187 returned by the <a href="../reference/open.htm">open</a> function. This file
188 object will be assigned to the '<nobr>file-stream</nobr>' variable.</p>
189
190 <p>It is now possible to write to this file by supplying the value of the
191 '<nobr>file-stream</nobr>' variable as the optional 'stream' parameter in
192 the <a href="../reference/print.htm">print</a> function.</p>
193
194 <pre class="example">
195 (print "Hello there" file-stream)
196 </pre>
197
198 <p>Evaluating this expression will result in the string <nobr>&quot;Hello
199 there&quot;</nobr> being written to the <nobr>file
200 &quot;test.dat&quot;</nobr>. More data can be written to the file using the
201 same technique.</p>
202
203 <p>Once you are done writing to the file, you should
204 <nobr><a href="../reference/close.htm">close</a> it</nobr>. Closing an
205 output file is just like closing an input file:</p>
206
207 <pre class="example">
208 (close file-stream)
209 </pre>
210
211 <p>Evaluating this expression will
212 <a href="../reference/close.htm">close</a> the output file and
213 make it permanent.</p>
214
215 <p>&nbsp;&nbsp;<a href="#top">Back to Top</a></p>
216
217 <a name="1-3"></a>
218
219 <hr>
220
221 <h2>1.3&nbsp; A Slightly More Complicated File Example</h2>
222
223 <hr>
224
225 <p>This example shows how to
226 <a href="../reference/open.htm">open</a> a file,
227 <a href="../reference/read.htm">read</a> each Lisp expression
228 from the file and <a href="../reference/print.htm">print</a> it.
229 It demonstrates the use of files and the use of the optional 'stream'
230 argument to the <a href="../reference/read.htm">read</a>
231 function.</p>
232
233 <pre class="example">
234 (do* ((file-stream (open "test.dat" :direction :input))
235 (expression (read file-stream) (read file-stream)))
236 ((null expression) nil)
237 (print expression))
238 </pre>
239
240 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
241
242 <a name="1-4"></a>
243
244 <hr>
245
246 <h2>1.4&nbsp; Closing Files via 'unwind-protect'</h2>
247
248 <hr>
249
250 <p>To make sure that the file gets closed in the end, the
251 <nobr>file i/o</nobr> functions can be wrapped by an <nobr><a
252 href="../reference/unwind-protect.htm">unwind-protect</a> form:</nobr></p>
253
254 <pre class="example">
255 (let ((file-stream (open "test.dat" :direction :input)))
256 (unwind-protect
257 <font color="#008844">;; protect-form</font>
258 (do ((expression (read file-stream) (read file-stream)))
259 ((null expression) nil)
260 (print expression))
261 <font color="#008844">;; clean-up form</font>
262 (when file-stream (close file-stream))))
263 </pre>
264
265 <p>This pattern can be found in many <nobr>file i/o</nobr> functions:</p>
266
267 <pre class="example">
268 (let ((<font color="#AA0000">file-stream</font> (open <font color="#0000CC">filename</font> :direction <font color="#0000CC">direction</font>)))
269 (unwind-protect
270 (progn
271 <font color="#008844">;; do something with the file-stream</font>
272 )
273 (when <font color="#AA0000">file-stream</font> (close <font color="#AA0000">file-stream</font>))))
274 </pre>
275
276 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
277
278 <a name="3"></a>
279
280 <hr>
281
282 <h2>3&nbsp; Testing Existence</h2>
283
284 <hr>
285
286 <p><div class="box">
287
288 <p>Note that these function are meant to prevent accidents during basic
289 <nobr>file i/o</nobr>, they may not be able to test if a file or directory
290 physically exists or is a link to a different place.</p>
291
292 </div></p>
293
294 <a name="directory-exists-p"></a>
295
296 <p>The Nyquist <a href="../reference/listdir.htm">listdir</a> function can
297 be used to test if a directory exists:</p>
298
299 <pre class="example">
300 (defun <font color="#0000CC">directory-exists-p</font> (string)
301 (and (listdir string) t))
302 </pre>
303
304 <a name="file-exists-p"></a>
305
306 <p>Testing if a file exists is a bit more tricky because on Unix [including
307 Linux and <nobr>Mac OS X]</nobr> a directory is a special kind of file, so
308 the XLISP <a href="../reference/open.htm">open</a> function also can open
309 directories. That's why we first must make sure that the filename string is
310 not the name of an existing directory:</p>
311
312 <pre class="example">
313 (defun <font color="#0000CC">file-exists-p</font> (string)
314 (or (stringp string) (error <font color="#880000">"not a string"</font> string))
315 (unless (listdir string)
316 (let ((file-stream (open string)))
317 (when file-stream
318 (close file-stream)
319 string))))
320 </pre>
321
322 <a name="file-or-directory-exists-p"></a>
323
324 <p>Before creating a new file it's always a good idea to check
325 if a file or directory with the same name already exists:</p>
326
327 <pre class="example">
328 (defun <font color="#0000CC">file-or-directory-exists-p</font> (string)
329 (or (stringp string) (error <font color="#880000">"not a string"</font> string))
330 (when (or (listdir string)
331 (let ((file-stream (open string)))
332 (when file-stream
333 (close file-stream)
334 t)))
335 string))
336 </pre>
337
338
339 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to Top</a></nobr></p>
340
341 <hr>
342
343 <a href="../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
344 <a href="../manual/contents.htm">Contents</a> |
345 <a href="tutorials.htm">Tutorials</a> |
346 <a href="../examples/examples.htm">Examples</a> |
347 <a href="../reference/reference-index.htm">Reference</a>
348
349 </body></html>