Reading-and-writing-whole-files.xhtml 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  3. <head>
  4. <title>Reading and writing whole files</title>
  5. <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
  6. <link rel="stylesheet" type="text/css" href="kawa.css"/>
  7. <script src="kawa-ebook.js" type="text/javascript"/>
  8. <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
  9. <link rel="prev" href="Overall-Index.xhtml" title="Index"/>
  10. <link rel="next" href="Ports.xhtml" title="Ports"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Reading and writing whole files" epub:type="subchapter" id="Reading-and-writing-whole-files">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Reading and writing whole files</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>The following procedures and syntax allow you to read and write
  23. the entire contents of a file, without iterating using a port.
  24. </p>
  25. <section class="sect2" title="Reading a file" epub:type="division" id="idm139667872829760">
  26. <div class="titlepage">
  27. <div>
  28. <div>
  29. <h3 class="title">Reading a file</h3>
  30. </div>
  31. </div>
  32. </div>
  33. <p>For reading the contents of a file in a single operation,
  34. you can use the following syntax:
  35. </p>
  36. <div class="literallayout">
  37. <p><code class="literal"><span class="bold"><strong>&amp;&lt;{</strong></span></code><a class="link" href="Named-quasi-literals.xhtml#meta-named-literal-part"><em class="replaceable"><code>named-literal-part</code></em></a>+<code class="literal"><span class="bold"><strong>}</strong></span></code><br/>
  38. </p>
  39. </div>
  40. <p>This is equivalent to using the <code class="literal">path-data</code> function (defined below):
  41. </p>
  42. <div class="literallayout">
  43. <p><code class="literal"><span class="bold"><strong>(path-data</strong></span></code> <code class="literal"><span class="bold"><strong>&amp;{</strong></span></code><a class="link" href="Named-quasi-literals.xhtml#meta-named-literal-part"><em class="replaceable"><code>named-literal-part</code></em></a>+<code class="literal"><span class="bold"><strong>})</strong></span></code><br/>
  44. </p>
  45. </div>
  46. <p>For example:
  47. </p>
  48. <pre class="screen">(define dir "/home/me/")
  49. (define help-message &amp;&lt;{&amp;[dir]HELP})
  50. </pre>
  51. <p>This binds <code class="literal">help-message</code> to the contents of the file
  52. named <code class="literal">HELP</code> in the <code class="literal">dir</code> directory.
  53. </p>
  54. <span id="Blobs"/>
  55. </section>
  56. <section class="sect2" title="Blobs" epub:type="division" id="idm139667872818656">
  57. <div class="titlepage">
  58. <div>
  59. <div>
  60. <h3 class="title">Blobs</h3>
  61. </div>
  62. </div>
  63. </div>
  64. <p>The content of a file is, in general, a sequence of uninterpreted bytes.
  65. Often these bytes represent text in a locale-dependent encoding,
  66. but we don’t always know this. Sometimes they’re images, or videos,
  67. or word-processor documents. A filename extension or a “magic number”
  68. in the file can give you hints, but not certainty as to the type of the data.
  69. </p>
  70. <p>A <em class="firstterm"><a class="ulink" href="http://en.wikipedia.org/wiki/Binary_large_object" target="_top">blob</a></em>
  71. is a raw uninterpreted sequence of bytes. It is a <code class="literal">bytevector</code>
  72. that can be automatically converted to other types as needed,
  73. specifically to a string or a bytevector.
  74. </p>
  75. <p>The <code class="literal">&amp;&lt;{..}</code> returns a blob. For example,
  76. assume the file <code class="literal">README</code> contains (bytes representing)
  77. the text <code class="literal">"Check doc directory.\n"</code>. Then:
  78. </p>
  79. <pre class="screen">#|kawa:1|# (define readme &amp;&lt;{README}))
  80. |kawa:2|# readme:class
  81. class gnu.lists.Blob
  82. #|kawa:3|# (write (-&gt;string readme))
  83. "Check doc directory.\n"
  84. #|kawa:4|# (write (-&gt;bytevector readme))
  85. #u8(67 104 101 99 107 32 100 111 99 32 100 105 114 101 99 116 111 114 121 46 10)
  86. #|kawa:5|# (-&gt;bytevector readme):class
  87. class gnu.lists.U8Vector
  88. </pre>
  89. </section>
  90. <section class="sect2" title="Writing to a file" epub:type="division" id="idm139667872811728">
  91. <div class="titlepage">
  92. <div>
  93. <div>
  94. <h3 class="title">Writing to a file</h3>
  95. </div>
  96. </div>
  97. </div>
  98. <p>The <code class="literal">&amp;&lt;{..}</code> syntax can be used with <code class="literal">set!</code>
  99. to replace the contents of a file:
  100. </p>
  101. <pre class="screen">(set! &amp;&lt;{README} "Check example.com\n")
  102. </pre>
  103. <p>The new contents must be blob-compatible - i.e. a bytevector or a string.
  104. </p>
  105. <p>If you dislike using <code class="literal">&lt;</code> as an output operator, you can instead using the <code class="literal">&amp;&gt;{..}</code> operation, which evaluates to a function whose single argument is the new value:
  106. </p>
  107. <pre class="screen">(&amp;&gt;{README} "Check example.com\n")
  108. </pre>
  109. <p>In general:
  110. </p>
  111. <pre class="screen"><code class="literal"><span class="bold"><strong>&amp;&gt;{</strong></span></code><a class="link" href="Named-quasi-literals.xhtml#meta-named-literal-part"><em class="replaceable"><code>named-literal-part</code></em></a>+<code class="literal"><span class="bold"><strong>}</strong></span></code>
  112. </pre>
  113. <p>is equivalent to:
  114. </p>
  115. <pre class="screen">(lambda (new-contents)
  116. (set! <code class="literal"><span class="bold"><strong>&amp;&lt;{</strong></span></code><a class="link" href="Named-quasi-literals.xhtml#meta-named-literal-part"><em class="replaceable"><code>named-literal-part</code></em></a>+<code class="literal"><span class="bold"><strong>}</strong></span></code> new-contents))
  117. </pre>
  118. <p>You can use <code class="literal">&amp;&gt;&gt;</code> to append more data to a file:
  119. </p>
  120. <pre class="screen">(&amp;&gt;&gt;{README} "or check example2.com\n")
  121. </pre>
  122. </section>
  123. <section class="sect2" title="Functions" epub:type="division" id="idm139667872799632">
  124. <div class="titlepage">
  125. <div>
  126. <div>
  127. <h3 class="title">Functions</h3>
  128. </div>
  129. </div>
  130. </div>
  131. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872798560" class="indexterm"/> <code class="function">path-data</code> <em class="replaceable"><code>path</code></em></p>
  132. <div class="blockquote">
  133. <blockquote class="blockquote">
  134. <p>Reads the contents of the file specified by <em class="replaceable"><code>path</code></em>,
  135. where <em class="replaceable"><code>path</code></em> can be a <a class="link" href="Paths.xhtml" title="Paths - file name, URLs, and URIs">path</a> object, or anything that can
  136. be converted to a <code class="literal">Path</code>, including a filename string or a URL.
  137. returning the result as a blob.
  138. The result is a <span class="emphasis"><em>blob</em></span>, which is a kind of bytevector than can be
  139. auto-converted to a string or bytevecor as required.
  140. </p>
  141. <p>The function <code class="literal">path-data</code> has a setter, which replaces the contents
  142. with new contents:
  143. </p>
  144. <pre class="screen">(set! &amp;&lt;{file-name} new-contents)
  145. </pre>
  146. </blockquote>
  147. </div>
  148. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667872791216" class="indexterm"/> <code class="function">path-bytes</code> <em class="replaceable"><code>path</code></em></p>
  149. <div class="blockquote">
  150. <blockquote class="blockquote">
  151. <p>Reads the contents of the file specified by <em class="replaceable"><code>path</code></em>, as
  152. with the <code class="literal">path-data</code> function, but the result is a plain bytevector,
  153. rather than a blob. This functtion also has a setter, which you
  154. can use to replace the file-contents by new bytevector-valued data.
  155. </p>
  156. </blockquote>
  157. </div>
  158. </section>
  159. </section>
  160. <footer>
  161. <div class="navfooter">
  162. <ul>
  163. <li>
  164. <b class="toc">
  165. <a href="Reading-and-writing-whole-files.xhtml#idm139667872829760">Reading a file</a>
  166. </b>
  167. </li>
  168. <li>
  169. <b class="toc">
  170. <a href="Reading-and-writing-whole-files.xhtml#idm139667872818656">Blobs</a>
  171. </b>
  172. </li>
  173. <li>
  174. <b class="toc">
  175. <a href="Reading-and-writing-whole-files.xhtml#idm139667872811728">Writing to a file</a>
  176. </b>
  177. </li>
  178. <li>
  179. <b class="toc">
  180. <a href="Reading-and-writing-whole-files.xhtml#idm139667872799632">Functions</a>
  181. </b>
  182. </li>
  183. </ul>
  184. <p>
  185. Up: <a accesskey="u" href="Input-Output.xhtml">Input, output, and file handling</a></p>
  186. <p>
  187. Previous: <a accesskey="p" href="Files.xhtml">File System Interface</a></p>
  188. <p>
  189. Next: <a accesskey="n" href="Ports.xhtml">Ports</a></p>
  190. </div>
  191. </footer>
  192. </body>
  193. </html>