86 lines
38 KiB
HTML
86 lines
38 KiB
HTML
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||
|
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=0.8"/><title>13.1.3 Port Buffers and Positions</title><link rel="stylesheet" type="text/css" href="../scribble.css" title="default"/><link rel="stylesheet" type="text/css" href="extras.css" title="default"/><link rel="stylesheet" type="text/css" href="../racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-style.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../doc-site.css" title="default"/><script type="text/javascript" src="../scribble-common.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../doc-site.js"></script><script type="text/javascript" src="../local-redirect/local-redirect.js"></script><script type="text/javascript" src="../local-redirect/local-user-redirect.js"></script><!--[if IE 6]><style type="text/css">.SIEHidden { overflow: hidden; }</style><![endif]--></head><body id="doc-racket-lang-org"><div class="tocset"><div class="tocview"><div class="tocviewlist tocviewlisttopspace"><div class="tocviewtitle"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,"tocview_0");">►</a></td><td></td><td><a href="index.html" class="tocviewlink" data-pltdoc="x">The Racket Reference</a></td></tr></table></div><div class="tocviewsublisttop" style="display: none;" id="tocview_0"><table cellspacing="0" cellpadding="0"><tr><td align="right">1 </td><td><a href="model.html" class="tocviewlink" data-pltdoc="x">Language Model</a></td></tr><tr><td align="right">2 </td><td><a href="notation.html" class="tocviewlink" data-pltdoc="x">Notation for Documentation</a></td></tr><tr><td align="right">3 </td><td><a href="syntax.html" class="tocviewlink" data-pltdoc="x">Syntactic Forms</a></td></tr><tr><td align="right">4 </td><td><a href="data.html" class="tocviewlink" data-pltdoc="x">Datatypes</a></td></tr><tr><td align="right">5 </td><td><a href="structures.html" class="tocviewlink" data-pltdoc="x">Structures</a></td></tr><tr><td align="right">6 </td><td><a href="mzlib_class.html" class="tocviewlink" data-pltdoc="x">Classes and Objects</a></td></tr><tr><td align="right">7 </td><td><a href="mzlib_unit.html" class="tocviewlink" data-pltdoc="x">Units</a></td></tr><tr><td align="right">8 </td><td><a href="contracts.html" class="tocviewlink" data-pltdoc="x">Contracts</a></td></tr><tr><td align="right">9 </td><td><a href="match.html" class="tocviewlink" data-pltdoc="x">Pattern Matching</a></td></tr><tr><td align="right">10 </td><td><a href="control.html" class="tocviewlink" data-pltdoc="x">Control Flow</a></td></tr><tr><td align="right">11 </td><td><a href="concurrency.html" class="tocviewlink" data-pltdoc="x">Concurrency and Parallelism</a></td></tr><tr><td align="right">12 </td><td><a href="Macros.html" class="tocviewlink" data-pltdoc="x">Macros</a></td></tr><tr><td align="right">13 </td><td><a href="input-and-output.html" class="tocviewselflink" data-pltdoc="x">Input and Output</a></td></tr><tr><td align="right">14 </td><td><a href="security.html" class="tocviewlink" data-pltdoc="x">Reflection and Security</a></td></tr><tr><td align="right">15 </td><td><a href="os.html" class="tocviewlink" data-pltdoc="x">Operating System</a></td></tr><tr><td align="right">16 </td><td><a href="memory.html" class="tocviewlink" data-pltdoc="x">Memory Management</a></td></tr><tr><td align="right">17 </td><td><a href="unsafe.html" class="tocviewlink" data-pltdoc="x">Unsafe Operations</a></td></tr><tr><td align="right">18 </td><td><a href="running.h
|
||
|
internally buffered:</p><ul><li><p>An input port is typically block-buffered by default, which
|
||
|
means that on any read, the buffer is filled with
|
||
|
immediately-available bytes to speed up future reads. Thus, if
|
||
|
a file is modified between a pair of reads to the file, the
|
||
|
second read can produce stale data. Calling
|
||
|
<span class="RktSym"><a href="port-buffers.html#%28def._%28%28quote._~23~25kernel%29._file-position%29%29" class="RktValLink" data-pltdoc="x">file-position</a></span> to set an input port’s file position
|
||
|
flushes its buffer.</p></li><li><p>An output port is typically block-buffered by default, though
|
||
|
a terminal output port is line-buffered, and the initial error
|
||
|
output port is unbuffered. An output buffer is filled with a
|
||
|
sequence of written bytes to be committed as a group, either
|
||
|
when the buffer is full (in block mode), when a newline is
|
||
|
written (in line mode), when the port is closed via
|
||
|
<span class="RktSym"><a href="port-ops.html#%28def._%28%28quote._~23~25kernel%29._close-output-port%29%29" class="RktValLink" data-pltdoc="x">close-output-port</a></span>, or when a flush is explicitly
|
||
|
requested via a procedure like <span class="RktSym"><a href="port-buffers.html#%28def._%28%28quote._~23~25kernel%29._flush-output%29%29" class="RktValLink" data-pltdoc="x">flush-output</a></span>.</p></li></ul><p>If a port supports buffering, its buffer mode can be changed via
|
||
|
<span class="RktSym"><a href="port-buffers.html#%28def._%28%28quote._~23~25kernel%29._file-stream-buffer-mode%29%29" class="RktValLink" data-pltdoc="x">file-stream-buffer-mode</a></span> (even if the port is not a
|
||
|
<a href="file-ports.html#%28tech._file._stream._port%29" class="techoutside" data-pltdoc="x"><span class="techinside">file-stream port</span></a>).</p><p>For an input port, peeking always places peeked bytes into the port’s
|
||
|
buffer, even when the port’s buffer mode is <span class="RktVal">'</span><span class="RktVal">none</span>;
|
||
|
furthermore, on some platforms, testing the port for input (via
|
||
|
<span class="RktSym"><a href="Byte_and_String_Input.html#%28def._%28%28quote._~23~25kernel%29._char-ready~3f%29%29" class="RktValLink" data-pltdoc="x">char-ready?</a></span> or <span class="RktSym"><a href="sync.html#%28def._%28%28quote._~23~25kernel%29._sync%29%29" class="RktValLink" data-pltdoc="x">sync</a></span>) may be implemented with a
|
||
|
peek. If an input port’s buffer mode is <span class="RktVal">'</span><span class="RktVal">none</span>, then at most
|
||
|
one byte is read for <span class="RktSym"><a href="Byte_and_String_Input.html#%28def._%28%28quote._~23~25kernel%29._read-bytes-avail%21%2A%29%29" class="RktValLink" data-pltdoc="x">read-bytes-avail!*</a></span>,
|
||
|
<span class="RktSym"><a href="Byte_and_String_Input.html#%28def._%28%28quote._~23~25kernel%29._read-bytes-avail%21%29%29" class="RktValLink" data-pltdoc="x">read-bytes-avail!</a></span>, <span class="RktSym"><a href="Byte_and_String_Input.html#%28def._%28%28quote._~23~25kernel%29._peek-bytes-avail%21%2A%29%29" class="RktValLink" data-pltdoc="x">peek-bytes-avail!*</a></span>, or
|
||
|
<span class="RktSym"><a href="Byte_and_String_Input.html#%28def._%28%28quote._~23~25kernel%29._peek-bytes-avail%21%29%29" class="RktValLink" data-pltdoc="x">peek-bytes-avail!</a></span>; if any bytes are buffered in the port
|
||
|
(e.g., to satisfy a previous peek), the procedures may access multiple
|
||
|
buffered bytes, but no further bytes are read.</p><p>In addition, the initial current output and error ports are
|
||
|
automatically flushed when they are terminal ports (see
|
||
|
<span class="RktSym"><a href="port-ops.html#%28def._%28%28quote._~23~25kernel%29._terminal-port~3f%29%29" class="RktValLink" data-pltdoc="x">terminal-port?</a></span>) and when <span class="RktSym"><a href="Reading.html#%28def._%28%28quote._~23~25kernel%29._read%29%29" class="RktValLink" data-pltdoc="x">read</a></span>, <span class="RktSym"><a href="Byte_and_String_Input.html#%28def._%28%28quote._~23~25kernel%29._read-line%29%29" class="RktValLink" data-pltdoc="x">read-line</a></span>,
|
||
|
<span class="RktSym"><a href="Byte_and_String_Input.html#%28def._%28%28quote._~23~25kernel%29._read-bytes%29%29" class="RktValLink" data-pltdoc="x">read-bytes</a></span>, <span class="RktSym"><a href="Byte_and_String_Input.html#%28def._%28%28quote._~23~25kernel%29._read-string%29%29" class="RktValLink" data-pltdoc="x">read-string</a></span>, etc., are performed on the
|
||
|
initial standard input port. (More precisely, instead of
|
||
|
<span class="RktSym"><a href="Reading.html#%28def._%28%28quote._~23~25kernel%29._read%29%29" class="RktValLink" data-pltdoc="x">read</a></span>, flushing is performed by the default port read handler;
|
||
|
see <span class="RktSym"><a href="Reading.html#%28def._%28%28quote._~23~25kernel%29._port-read-handler%29%29" class="RktValLink" data-pltdoc="x">port-read-handler</a></span>.)</p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((quote._~23~25kernel)._flush-output))"></a><span title="Provided from: racket/base, racket | Package: base"><span class="RktSym"><a href="port-buffers.html#%28def._%28%28quote._~23~25kernel%29._flush-output%29%29" class="RktValDef RktValLink" data-pltdoc="x">flush-output</a></span></span><span class="hspace"> </span><span class="RktOpt">[</span><span class="RktVar">out</span><span class="RktOpt">]</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="void.html#%28def._%28%28quote._~23~25kernel%29._void~3f%29%29" class="RktValLink" data-pltdoc="x">void?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">out</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="port-ops.html#%28def._%28%28quote._~23~25kernel%29._output-port~3f%29%29" class="RktValLink" data-pltdoc="x">output-port?</a></span><span class="hspace"> </span>=<span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="port-ops.html#%28def._%28%28quote._~23~25kernel%29._current-output-port%29%29" class="RktValLink" data-pltdoc="x">current-output-port</a></span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara"><a name="(idx._(gentag._244._(lib._scribblings/reference/reference..scrbl)))"></a>Forces all buffered data in the given
|
||
|
output port to be physically written. Only <a href="file-ports.html#%28tech._file._stream._port%29" class="techoutside" data-pltdoc="x"><span class="techinside">file-stream ports</span></a>,
|
||
|
TCP ports, and custom ports (see <a href="customport.html" data-pltdoc="x">Custom Ports</a>) use
|
||
|
buffers; when called on a port without a buffer, <span class="RktSym"><a href="port-buffers.html#%28def._%28%28quote._~23~25kernel%29._flush-output%29%29" class="RktValLink" data-pltdoc="x">flush-output</a></span>
|
||
|
has no effect.</div></p><p>If flushing a <a href="file-ports.html#%28tech._file._stream._port%29" class="techoutside" data-pltdoc="x"><span class="techinside">file-stream port</span></a> or <a href="tcp.html#%28tech._tcp._port%29" class="techoutside" data-pltdoc="x"><span class="techinside">TCP port</span></a> encounters an
|
||
|
error when writing, then all buffered bytes in the port are discarded.
|
||
|
Consequently, a further attempt to flush or close the port will not
|
||
|
fail.</p><p class="SHistory">Changed in version 7.4.0.10 of package <span class="stt">base</span>: Consistently, discard buffered bytes on error,
|
||
|
including in a TCP output port.</p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((quote._~23~25kernel)._file-stream-buffer-mode))"></a><span title="Provided from: racket/base, racket | Package: base"><span class="RktSym"><a href="port-buffers.html#%28def._%28%28quote._~23~25kernel%29._file-stream-buffer-mode%29%29" class="RktValDef RktValLink" data-pltdoc="x">file-stream-buffer-mode</a></span></span><span class="hspace"> </span><span class="RktVar">port</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._or%2Fc%29%29" class="RktValLink" data-pltdoc="x">or/c</a></span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">none</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">line</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">block</span><span class="hspace"> </span><span class="RktVal">#f</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">port</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="port-ops.html#%28def._%28%28lib._racket%2Fprivate%2Fmisc..rkt%29._port~3f%29%29" class="RktValLink" data-pltdoc="x">port?</a></span></td></tr><tr><td><span class="RktPn">(</span><span title="Provided from: racket/base, racket | Package: base"><span class="RktSym"><a href="port-buffers.html#%28def._%28%28quote._~23~25kernel%29._file-stream-buffer-mode%29%29" class="RktValDef RktValLink" data-pltdoc="x">file-stream-buffer-mode</a></span></span><span class="hspace"> </span><span class="RktVar">port</span><span class="hspace"> </span><span class="RktVar">mode</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="void.html#%28def._%28%28quote._~23~25kernel%29._void~3f%29%29" class="RktValLink" data-pltdoc="x">void?</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">port</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="port-ops.html#%28def._%28%28lib._racket%2Fprivate%2Fmisc..rkt%29._port~3f%29%29" class="RktValLink" data-pltdoc="x">port?</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">mode</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._or%2Fc%29%29" class="RktValLink" data-pltdoc="x">or/c</a></span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">none</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">line</span><span class="hspace"> </span><span class="RktVal">'</span><span class="RktVal">block</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Gets or sets the buffer mode for <span class="RktVar">port</span>, if
|
||
|
possible. <a href="file-ports.html#%28tech._file._stream._port%29" class="techoutside" data-pltdoc="x"><span class="techinside">File-stream ports</span></a> support setting the buffer mode,
|
||
|
TCP ports (see <a href="networking.html" data-pltdoc="x">Networking</a>) support setting and getting
|
||
|
the buffer mode, and custom ports (see <a href="customport.html" data-pltdoc="x">Custom Ports</a>) may
|
||
|
support getting and setting buffer modes.</div></p><p>If <span class="RktVar">mode</span> is provided, it must be one of
|
||
|
<a name="(idx._(gentag._245._(lib._scribblings/reference/reference..scrbl)))"></a><span class="RktVal">'</span><span class="RktVal">none</span>, <a name="(idx._(gentag._246._(lib._scribblings/reference/reference..scrbl)))"></a><span class="RktVal">'</span><span class="RktVal">line</span> (output only), or
|
||
|
<a name="(idx._(gentag._247._(lib._scribblings/reference/reference..scrbl)))"></a><span class="RktVal">'</span><span class="RktVal">block</span>, and the port’s buffering is set
|
||
|
accordingly. If the port does not support setting the mode, the
|
||
|
<span class="RktSym"><a href="exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail%29%29" class="RktValLink" data-pltdoc="x">exn:fail</a></span> exception is raised.</p><p>If <span class="RktVar">mode</span> is not provided, the current mode is returned, or
|
||
|
<span class="RktVal">#f</span> is returned if the mode cannot be determined. If
|
||
|
<span class="RktVar">port</span> is an input port and <span class="RktVar">mode</span> is
|
||
|
<span class="RktVal">'</span><span class="RktVal">line</span>, the <span class="RktSym"><a href="exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract%29%29" class="RktValLink" data-pltdoc="x">exn:fail:contract</a></span> exception is raised.</p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((quote._~23~25kernel)._file-position))"></a><span title="Provided from: racket/base, racket | Package: base"><span class="RktSym"><a href="port-buffers.html#%28def._%28%28quote._~23~25kernel%29._file-position%29%29" class="RktValDef RktValLink" data-pltdoc="x">file-position</a></span></span><span class="hspace"> </span><span class="RktVar">port</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="number-types.html#%28def._%28%28quote._~23~25kernel%29._exact-nonnegative-integer~3f%29%29" class="RktValLink" data-pltdoc="x">exact-nonnegative-integer?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">port</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="port-ops.html#%28def._%28%28lib._racket%2Fprivate%2Fmisc..rkt%29._port~3f%29%29" class="RktValLink" data-pltdoc="x">port?</a></span></td></tr><tr><td><span class="RktPn">(</span><span title="Provided from: racket/base, racket | Package: base"><span class="RktSym"><a href="port-buffers.html#%28def._%28%28quote._~23~25kernel%29._file-position%29%29" class="RktValDef RktValLink" data-pltdoc="x">file-position</a></span></span><span class="hspace"> </span><span class="RktVar">port</span><span class="hspace"> </span><span class="RktVar">pos</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="void.html#%28def._%28%28quote._~23~25kernel%29._void~3f%29%29" class="RktValLink" data-pltdoc="x">void?</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">port</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="port-ops.html#%28def._%28%28lib._racket%2Fprivate%2Fmisc..rkt%29._port~3f%29%29" class="RktValLink" data-pltdoc="x">port?</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">pos</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._or%2Fc%29%29" class="RktValLink" data-pltdoc="x">or/c</a></span><span class="hspace"> </span><span class="RktSym"><a href="number-types.html#%28def._%28%28quote._~23~25kernel%29._exact-nonnegative-integer~3f%29%29" class="RktValLink" data-pltdoc="x">exact-nonnegative-integer?</a></span><span class="hspace"> </span><span class="RktSym"><a href="port-ops.html#%28def._%28%28quote._~23~25kernel%29._eof-object~3f%29%29" class="RktValLink" data-pltdoc="x">eof-object?</a></span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">Returns or sets the current read/write position of <span class="RktVar">port</span>.</div></p><p>Calling <span class="RktSym"><a href="port-buffers.html#%28def._%28%28quote._~23~25kernel%29._file-position%29%29" class="RktValLink" data-pltdoc="x">file-position</a></span> without a position on a port other
|
||
|
than a <a href="file-ports.html#%28tech._file._stream._port%29" class="techoutside" data-pltdoc="x"><span class="techinside">file-stream port</span></a> or <a href="stringport.html#%28tech._string._port%29" class="techoutside" data-pltdoc="x"><span class="techinside">string port</span></a>
|
||
|
returns the number of bytes that have
|
||
|
been read from that port if the position is known (see
|
||
|
<a href="linecol.html" data-pltdoc="x">Counting Positions, Lines, and Columns</a>), otherwise the <span class="RktSym"><a href="exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3afilesystem%29%29" class="RktValLink" data-pltdoc="x">exn:fail:filesystem</a></span> exception is raised.</p><p>For <a href="file-ports.html#%28tech._file._stream._port%29" class="techoutside" data-pltdoc="x"><span class="techinside">file-stream ports</span></a> and <a href="stringport.html#%28tech._string._port%29" class="techoutside" data-pltdoc="x"><span class="techinside">string ports</span></a>, the position-setting
|
||
|
variant sets the read/write position to <span class="RktVar">pos</span> relative to the
|
||
|
beginning of the file or (byte) string if <span class="RktVar">pos</span> is a number, or to the
|
||
|
current end of the file or (byte) string if <span class="RktVar">pos</span> is <span class="RktSym"><a href="port-ops.html#%28def._%28%28quote._~23~25kernel%29._eof%29%29" class="RktValLink" data-pltdoc="x">eof</a></span>. In
|
||
|
position-setting mode, <span class="RktSym"><a href="port-buffers.html#%28def._%28%28quote._~23~25kernel%29._file-position%29%29" class="RktValLink" data-pltdoc="x">file-position</a></span> raises the
|
||
|
<span class="RktSym"><a href="exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3acontract%29%29" class="RktValLink" data-pltdoc="x">exn:fail:contract</a></span> exception for port kinds other than
|
||
|
<a href="file-ports.html#%28tech._file._stream._port%29" class="techoutside" data-pltdoc="x"><span class="techinside">file-stream ports</span></a> and <a href="stringport.html#%28tech._string._port%29" class="techoutside" data-pltdoc="x"><span class="techinside">string ports</span></a>. Furthermore, not all <a href="file-ports.html#%28tech._file._stream._port%29" class="techoutside" data-pltdoc="x"><span class="techinside">file-stream
|
||
|
ports</span></a> support setting the position; if <span class="RktSym"><a href="port-buffers.html#%28def._%28%28quote._~23~25kernel%29._file-position%29%29" class="RktValLink" data-pltdoc="x">file-position</a></span> is
|
||
|
called with a position argument on such a <a href="file-ports.html#%28tech._file._stream._port%29" class="techoutside" data-pltdoc="x"><span class="techinside">file-stream port</span></a>, the
|
||
|
<span class="RktSym"><a href="exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail~3afilesystem%29%29" class="RktValLink" data-pltdoc="x">exn:fail:filesystem</a></span> exception is raised.</p><p>When <span class="RktSym"><a href="port-buffers.html#%28def._%28%28quote._~23~25kernel%29._file-position%29%29" class="RktValLink" data-pltdoc="x">file-position</a></span> sets the position <span class="RktVar">pos</span> beyond the
|
||
|
current size of an output file or (byte) string, the file/string is
|
||
|
enlarged to size <span class="RktVar">pos</span> and the new region is filled with
|
||
|
<span class="RktVal">0</span> bytes; in the case of a file. In the case of a file output
|
||
|
port, the file might not be enlarged until more data is written to the
|
||
|
file; in that case, beware that writing to a file opened in
|
||
|
<span class="RktVal">'</span><span class="RktVal">append</span> mode on Unix and Mac OS will reset the file pointer
|
||
|
to the end of a file <span class="emph">before</span> each write, which defeats file
|
||
|
enlargement via <span class="RktSym"><a href="port-buffers.html#%28def._%28%28quote._~23~25kernel%29._file-position%29%29" class="RktValLink" data-pltdoc="x">file-position</a></span>. If <span class="RktVar">pos</span> is beyond the
|
||
|
end of an input file or (byte) string, then reading thereafter returns
|
||
|
<span class="RktSym"><a href="port-ops.html#%28def._%28%28quote._~23~25kernel%29._eof%29%29" class="RktValLink" data-pltdoc="x">eof</a></span> without changing the port’s position.</p><p>When changing the file position for an output port, the port is first
|
||
|
flushed if its buffer is not empty. Similarly, setting the position
|
||
|
for an input port clears the port’s buffer (even if the new position
|
||
|
is the same as the old position). However, although input and output
|
||
|
ports produced by <span class="RktSym"><a href="file-ports.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._open-input-output-file%29%29" class="RktValLink" data-pltdoc="x">open-input-output-file</a></span> share the file
|
||
|
position, setting the position via one port does not flush the other
|
||
|
port’s buffer.</p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((quote._~23~25kernel)._file-position*))"></a><span title="Provided from: racket/base, racket | Package: base"><span class="RktSym"><a href="port-buffers.html#%28def._%28%28quote._~23~25kernel%29._file-position%2A%29%29" class="RktValDef RktValLink" data-pltdoc="x">file-position*</a></span></span><span class="hspace"> </span><span class="RktVar">port</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._or%2Fc%29%29" class="RktValLink" data-pltdoc="x">or/c</a></span><span class="hspace"> </span><span class="RktSym"><a href="number-types.html#%28def._%28%28quote._~23~25kernel%29._exact-nonnegative-integer~3f%29%29" class="RktValLink" data-pltdoc="x">exact-nonnegative-integer?</a></span><span class="hspace"> </span><span class="RktVal">#f</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">port</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="port-ops.html#%28def._%28%28lib._racket%2Fprivate%2Fmisc..rkt%29._port~3f%29%29" class="RktValLink" data-pltdoc="x">port?</a></span></td></tr></table></blockquote></div><div class="SIntrapara">Like <span class="RktSym"><a href="port-buffers.html#%28def._%28%28quote._~23~25kernel%29._file-position%29%29" class="RktValLink" data-pltdoc="x">file-position</a></span> on a single argument, but returns
|
||
|
<span class="RktVal">#f</span> if the position is not known.</div></p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>procedure</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((quote._~23~25kernel)._file-truncate))"></a><span title="Provided from: racket/base, racket | Package: base"><span class="RktSym"><a href="port-buffers.html#%28def._%28%28quote._~23~25kernel%29._file-truncate%29%29" class="RktValDef RktValLink" data-pltdoc="x">file-truncate</a></span></span><span class="hspace"> </span><span class="RktVar">port</span><span class="hspace"> </span><span class="RktVar">size</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="void.html#%28def._%28%28quote._~23~25kernel%29._void~3f%29%29" class="RktValLink" data-pltdoc="x">void?</a></span></p></blockquote></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">port</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._and%2Fc%29%29" class="RktValLink" data-pltdoc="x">and/c</a></span><span class="hspace"> </span><span class="RktSym"><a href="port-ops.html#%28def._%28%28quote._~23~25kernel%29._output-port~3f%29%29" class="RktValLink" data-pltdoc="x">output-port?</a></span><span class="hspace"> </span><span class="RktSym"><a href="port-ops.html#%28def._%28%28quote._~23~25kernel%29._file-stream-port~3f%29%29" class="RktValLink" data-pltdoc="x">file-stream-port?</a></span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">size</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktSym"><a href="number-types.html#%28def._%28%28quote._~23~25kernel%29._exact-nonnegative-integer~3f%29%29" class="RktValLink" data-pltdoc="x">exact-nonnegative-integer?</a></span></td></tr></table></blockquote></div><div class="SIntrapara">Sets the size of the file written by <span class="RktVar">port</span> to <span class="RktVar">size</span>,
|
||
|
assuming that the port is associated to a file whose size can be set.</div></p><p>The new file size can be either larger or smaller than its current
|
||
|
size, but “truncate” in this function’s name reflects that it is
|
||
|
normally used to decrease the size of a file, since writing to a file
|
||
|
or using <span class="RktSym"><a href="port-buffers.html#%28def._%28%28quote._~23~25kernel%29._file-position%29%29" class="RktValLink" data-pltdoc="x">file-position</a></span> can extend a file’s size.</p><div class="navsetbottom"><span class="navleft"><form class="searchform"><input class="searchbox" id="searchbox" type="text" tabindex="1" placeholder="...search manuals..." title="Enter a search string to search the manuals" onkeypress="return DoSearchKey(event, this, "8.6", "../");"/></form> <a href="https://docs.racket-lang.org/index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot("8.6");">top</a><span class="tocsettoggle"> <a href="javascript:void(0);" title="show/hide table of contents" onclick="TocsetToggle();">contents</a></span></span><span class="navright"> <a href="port-ops.html" title="backward to "13.1.2 Managing Ports"" data-pltdoc="x">← prev</a> <a href="ports.html" title="up to "13.1 Ports"" data-pltdoc="x">up</a> <a href="linecol.html" title="forward to "13.1.4 Counting Positions, Lines, and Columns"" data-pltdoc="x">next →</a></span> </div></div></div><div id="contextindicator"> </div></body></html>
|