High-order abstract I/O.
IO module simply deals with abstract inputs/outputs. It provides a set of methods for working with these IO as well as several constructors that enable to write to an underlying channel, buffer, or enum.
The abstract input type.
The abstract output type, 'a
is the accumulator data, it is returned
when the close_out
function is called.
This exception is raised when reading on an input with the read
or
nread
functions while there is no available token to read.
This exception is raised when reading on a closed input.
This exception is raised when reading on a closed output.
Read a single char from an input or raise No_more_input
if
no input available.
nread i n
reads a string of size up to n
from an input.
The function will raise No_more_input
if no input is available.
It will raise Invalid_argument
if n
< 0.
really_nread i n
reads a string of exactly n
characters
from the input. Raises No_more_input
if at least n
characters are
not available. Raises Invalid_argument
if n
< 0.
input i s p l
reads up to l
characters from the given input, storing
them in string s
, starting at character number p
. It returns the actual
number of characters read or raise No_more_input
if no character can be
read. It will raise Invalid_argument
if p
and l
do not designate a
valid substring of s
.
really_input i s p l
reads exactly l
characters from the given input,
storing them in the string s
, starting at position p
. For consistency with
IO.input it returns l
. Raises No_more_input
if at l
characters are
not available. Raises Invalid_argument
if p
and l
do not designate a
valid substring of s
.
output o s p l
writes up to l
characters from string s
, starting at
offset p
. It returns the number of characters written. It will raise
Invalid_argument
if p
and l
do not designate a valid substring of s
.
Close the output and return its accumulator data. It can no longer be written.
Create an output that will write into a string in an efficient way. When closed, the output returns all the data written into it.
Create an output that will write into a string in an efficient way. When closed, the output returns all the data written into it. Several strings are used in case the output size excess max_string_length
Create an output that will write into a channel.
Fully create an input by giving all the needed functions.
Fully create an output by giving all the needed functions.
The printf function works for any output.
Here is some API useful for working with binary files, in particular binary files generated by C applications. By default, encoding of multibyte integers is low-endian. The BigEndian module provide multibyte operations with other encoding.
Exception raised when a read or write operation cannot be completed.
Read a signed 32-bit integer. Raise Overflow
if the
read integer cannot be represented as a Caml 31-bit integer.
Write an IEEE single precision floating point value (32 bits).
Write an IEEE double precision floating point value (64 bits).
Write a line and append a LF (it might be converted to CRLF on some systems depending on the underlying IO).
Same as operations above, but use big-endian encoding
This enable you to read and write from an IO bit-by-bit or several bits at the same time.
Write up to 31 bits represented as a value, raise Bits_error if nbits < 0 or nbits > 31 or the value representation excess nbits.
Flush remaining unwritten bits, adding up to 7 bits which values 0.
Theses OO Wrappers have been written to provide easy support of ExtLib
IO by external librairies. If you want your library to support ExtLib
IO without actually requiring ExtLib to compile, you can should implement
the classes in_channel
, out_channel
, poly_in_channel
and/or
poly_out_channel
which are the common IO specifications established
for ExtLib, OCamlNet and Camomile.
(see http://www.ocaml-programming.de/tmp/IO-Classes.html for more details).