moonpool/dev/ocaml/Stdlib/In_channel/index.html

3 lines
No EOL
18 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>In_channel (ocaml.Stdlib.In_channel)</title><link rel="stylesheet" href="../../../_odoc-theme/odoc.css"/><meta charset="utf-8"/><meta name="generator" content="odoc 2.2.1"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../../highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body class="odoc"><nav class="odoc-nav"><a href="../index.html">Up</a> <a href="../../index.html">ocaml</a> &#x00BB; <a href="../index.html">Stdlib</a> &#x00BB; In_channel</nav><header class="odoc-preamble"><h1>Module <code><span>Stdlib.In_channel</span></code></h1><p>Input channels.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.14.0</li></ul></header><div class="odoc-content"><div class="odoc-spec"><div class="spec type anchored" id="type-t"><a href="#type-t" class="anchor"></a><code><span><span class="keyword">type</span> t</span><span> = <a href="../index.html#type-in_channel">in_channel</a></span></code></div><div class="spec-doc"><p>The type of input channel.</p></div></div><div class="odoc-spec"><div class="spec type anchored" id="type-open_flag"><a href="#type-open_flag" class="anchor"></a><code><span><span class="keyword">type</span> open_flag</span><span> = <a href="../index.html#type-open_flag">open_flag</a></span><span> = </span></code><ol><li id="type-open_flag.Open_rdonly" class="def variant constructor anchored"><a href="#type-open_flag.Open_rdonly" class="anchor"></a><code><span>| </span><span><span class="constructor">Open_rdonly</span></span></code><div class="def-doc"><span class="comment-delim">(*</span><p>open for reading.</p><span class="comment-delim">*)</span></div></li><li id="type-open_flag.Open_wronly" class="def variant constructor anchored"><a href="#type-open_flag.Open_wronly" class="anchor"></a><code><span>| </span><span><span class="constructor">Open_wronly</span></span></code><div class="def-doc"><span class="comment-delim">(*</span><p>open for writing.</p><span class="comment-delim">*)</span></div></li><li id="type-open_flag.Open_append" class="def variant constructor anchored"><a href="#type-open_flag.Open_append" class="anchor"></a><code><span>| </span><span><span class="constructor">Open_append</span></span></code><div class="def-doc"><span class="comment-delim">(*</span><p>open for appending: always write at end of file.</p><span class="comment-delim">*)</span></div></li><li id="type-open_flag.Open_creat" class="def variant constructor anchored"><a href="#type-open_flag.Open_creat" class="anchor"></a><code><span>| </span><span><span class="constructor">Open_creat</span></span></code><div class="def-doc"><span class="comment-delim">(*</span><p>create the file if it does not exist.</p><span class="comment-delim">*)</span></div></li><li id="type-open_flag.Open_trunc" class="def variant constructor anchored"><a href="#type-open_flag.Open_trunc" class="anchor"></a><code><span>| </span><span><span class="constructor">Open_trunc</span></span></code><div class="def-doc"><span class="comment-delim">(*</span><p>empty the file if it already exists.</p><span class="comment-delim">*)</span></div></li><li id="type-open_flag.Open_excl" class="def variant constructor anchored"><a href="#type-open_flag.Open_excl" class="anchor"></a><code><span>| </span><span><span class="constructor">Open_excl</span></span></code><div class="def-doc"><span class="comment-delim">(*</span><p>fail if Open_creat and the file already exists.</p><span class="comment-delim">*)</span></div></li><li id="type-open_flag.Open_binary" class="def variant constructor anchored"><a href="#type-open_flag.Open_binary" class="anchor"></a><code><span>| </span><span><span class="constructor">Open_binary</span></span></code><div class="def-doc"><span class="comment-delim">(*</span><p>open in binary mode (no conversion).</p><span class="comment-delim">*)</span></div></li><li id="type-open_flag.Open_text" class="def variant constructor anchored"><a href="#type-open_flag.Open_text" class="anchor"></a><code><span>| </span><span><span class="constructor">Open_text</span></span></code><div class="def-doc"><span class="comment-delim">(*</span><p>open in text mode (may perform conversions).</p><span class="comment-delim">*)</span></div></li><li id="type-open_flag.Open_nonblock" class="def variant constructor anchored"><a href="#type-open_flag.Open_nonblock" class="anchor"></a><code><span>| </span><span><span class="constructor">Open_nonblock</span></span></code><div class="def-doc"><span class="comment-delim">(*</span><p>open in non-blocking mode.</p><span class="comment-delim">*)</span></div></li></ol></div><div class="spec-doc"><p>Opening modes for <a href="#val-open_gen"><code>open_gen</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-stdin"><a href="#val-stdin" class="anchor"></a><code><span><span class="keyword">val</span> stdin : <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>The standard input for the process.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-open_bin"><a href="#val-open_bin" class="anchor"></a><code><span><span class="keyword">val</span> open_bin : <span>string <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Open the named file for reading, and return a new input channel on that file, positioned at the beginning of the file.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-open_text"><a href="#val-open_text" class="anchor"></a><code><span><span class="keyword">val</span> open_text : <span>string <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Same as <a href="#val-open_bin"><code>open_bin</code></a>, but the file is opened in text mode, so that newline translation takes place during reads. On operating systems that do not distinguish between text mode and binary mode, this function behaves like <a href="#val-open_bin"><code>open_bin</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-open_gen"><a href="#val-open_gen" class="anchor"></a><code><span><span class="keyword">val</span> open_gen : <span><span><a href="#type-open_flag">open_flag</a> list</span> <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <span>string <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>open_gen mode perm filename</code> opens the named file for reading, as described above. The extra arguments <code>mode</code> and <code>perm</code> specify the opening mode and file permissions. <a href="#val-open_text"><code>open_text</code></a> and <a href="#val-open_bin"><code>open_bin</code></a> are special cases of this function.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-with_open_bin"><a href="#val-with_open_bin" class="anchor"></a><code><span><span class="keyword">val</span> with_open_bin : <span>string <span class="arrow">&#45;&gt;</span></span> <span><span>(<span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span>)</span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p><code>with_open_bin fn f</code> opens a channel <code>ic</code> on file <code>fn</code> and returns <code>f
ic</code>. After <code>f</code> returns, either with a value or by raising an exception, <code>ic</code> is guaranteed to be closed.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-with_open_text"><a href="#val-with_open_text" class="anchor"></a><code><span><span class="keyword">val</span> with_open_text : <span>string <span class="arrow">&#45;&gt;</span></span> <span><span>(<span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span>)</span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Like <a href="#val-with_open_bin"><code>with_open_bin</code></a>, but the channel is opened in text mode (see <a href="#val-open_text"><code>open_text</code></a>).</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-with_open_gen"><a href="#val-with_open_gen" class="anchor"></a><code><span><span class="keyword">val</span> with_open_gen : <span><span><a href="#type-open_flag">open_flag</a> list</span> <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <span>string <span class="arrow">&#45;&gt;</span></span> <span><span>(<span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span>)</span> <span class="arrow">&#45;&gt;</span></span> <span class="type-var">'a</span></span></code></div><div class="spec-doc"><p>Like <a href="#val-with_open_bin"><code>with_open_bin</code></a>, but can specify the opening mode and file permission, in case the file must be created (see <a href="#val-open_gen"><code>open_gen</code></a>).</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-seek"><a href="#val-seek" class="anchor"></a><code><span><span class="keyword">val</span> seek : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>int64 <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p><code>seek chan pos</code> sets the current reading position to <code>pos</code> for channel <code>chan</code>. This works only for regular files. On files of other kinds, the behavior is unspecified.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-pos"><a href="#val-pos" class="anchor"></a><code><span><span class="keyword">val</span> pos : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> int64</span></code></div><div class="spec-doc"><p>Return the current reading position for the given channel. For files opened in text mode under Windows, the returned position is approximate (owing to end-of-line conversion); in particular, saving the current position with <a href="#val-pos"><code>pos</code></a>, then going back to this position using <a href="#val-seek"><code>seek</code></a> will not work. For this programming idiom to work reliably and portably, the file must be opened in binary mode.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-length"><a href="#val-length" class="anchor"></a><code><span><span class="keyword">val</span> length : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> int64</span></code></div><div class="spec-doc"><p>Return the size (number of characters) of the regular file on which the given channel is opened. If the channel is opened on a file that is not a regular file, the result is meaningless. The returned size does not take into account the end-of-line translations that can be performed when reading from a channel opened in text mode.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-close"><a href="#val-close" class="anchor"></a><code><span><span class="keyword">val</span> close : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Close the given channel. Input functions raise a <code>Sys_error</code> exception when they are applied to a closed input channel, except <a href="#val-close"><code>close</code></a>, which does nothing when applied to an already closed channel.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-close_noerr"><a href="#val-close_noerr" class="anchor"></a><code><span><span class="keyword">val</span> close_noerr : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Same as <a href="#val-close"><code>close</code></a>, but ignore all errors.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-input_char"><a href="#val-input_char" class="anchor"></a><code><span><span class="keyword">val</span> input_char : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>char option</span></span></code></div><div class="spec-doc"><p>Read one character from the given input channel. Returns <code>None</code> if there are no more characters to read.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-input_byte"><a href="#val-input_byte" class="anchor"></a><code><span><span class="keyword">val</span> input_byte : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>int option</span></span></code></div><div class="spec-doc"><p>Same as <a href="#val-input_char"><code>input_char</code></a>, but return the 8-bit integer representing the character. Returns <code>None</code> if the end of file was reached.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-input_line"><a href="#val-input_line" class="anchor"></a><code><span><span class="keyword">val</span> input_line : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>string option</span></span></code></div><div class="spec-doc"><p><code>input_line ic</code> reads characters from <code>ic</code> until a newline or the end of file is reached. Returns the string of all characters read, without the newline (if any). Returns <code>None</code> if the end of the file has been reached. In particular, this will be the case if the last line of input is empty.</p><p>A newline is the character <code>\n</code> unless the file is open in text mode and <a href="../Sys/index.html#val-win32"><code>Sys.win32</code></a> is <code>true</code> in which case it is the sequence of characters <code>\r\n</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-input"><a href="#val-input" class="anchor"></a><code><span><span class="keyword">val</span> input : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>bytes <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> int</span></code></div><div class="spec-doc"><p><code>input ic buf pos len</code> reads up to <code>len</code> characters from the given channel <code>ic</code>, storing them in byte sequence <code>buf</code>, starting at character number <code>pos</code>. It returns the actual number of characters read, between 0 and <code>len</code> (inclusive). A return value of 0 means that the end of file was reached.</p><p>Use <a href="#val-really_input"><code>really_input</code></a> to read exactly <code>len</code> characters.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if <code>pos</code> and <code>len</code> do not designate a valid range of <code>buf</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-really_input"><a href="#val-really_input" class="anchor"></a><code><span><span class="keyword">val</span> really_input : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>bytes <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <span>unit option</span></span></code></div><div class="spec-doc"><p><code>really_input ic buf pos len</code> reads <code>len</code> characters from channel <code>ic</code>, storing them in byte sequence <code>buf</code>, starting at character number <code>pos</code>.</p><p>Returns <code>None</code> if the end of file is reached before <code>len</code> characters have been read.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <span class="value">Invalid_argument</span> <p>if <code>pos</code> and <code>len</code> do not designate a valid range of <code>buf</code>.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-really_input_string"><a href="#val-really_input_string" class="anchor"></a><code><span><span class="keyword">val</span> really_input_string : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <span>string option</span></span></code></div><div class="spec-doc"><p><code>really_input_string ic len</code> reads <code>len</code> characters from channel <code>ic</code> and returns them in a new string. Returns <code>None</code> if the end of file is reached before <code>len</code> characters have been read.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-input_all"><a href="#val-input_all" class="anchor"></a><code><span><span class="keyword">val</span> input_all : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> string</span></code></div><div class="spec-doc"><p><code>input_all ic</code> reads all remaining data from <code>ic</code>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-set_binary_mode"><a href="#val-set_binary_mode" class="anchor"></a><code><span><span class="keyword">val</span> set_binary_mode : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span>bool <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p><code>set_binary_mode ic true</code> sets the channel <code>ic</code> to binary mode: no translations take place during input.</p><p><code>set_binary_mode ic false</code> sets the channel <code>ic</code> to text mode: depending on the operating system, some translations may take place during input. For instance, under Windows, end-of-lines will be translated from <code>\r\n</code> to <code>\n</code>.</p><p>This function has no effect under operating systems that do not distinguish between text mode and binary mode.</p></div></div></div></body></html>