package stdio

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

In_channel collects all of the pervasive functions that work on in_channels.

  • It adds some new functions (like input_all and input_lines).
  • It names things using the fact that there is no worry about toplevel name conflicts (since we are in a module).
  • It uses labelled arguments.
  • It returns an option rather than raising End_of_file.

Note that an in_channel is a custom block with a finalizer, and so is allocated directly to the major heap. Creating a lot of in_channels can result in many major collections and poor performance.

val stdin : t

Channels are opened in binary mode iff binary is true. This only has an effect on Windows.

val create : ?binary:Base.Bool.t -> Base.String.t -> t
val with_file : ?binary:Base.Bool.t -> Base.String.t -> f:(t -> 'a) -> 'a

with_file ~f fname executes ~f on the open channel from fname, and closes it afterwards.

val close : t -> Base.Unit.t

close t closes t, or does nothing if t is already closed, and may raise an exception.

val input : t -> buf:Base.String.t -> pos:Base.Int.t -> len:Base.Int.t -> Base.Int.t
val really_input : t -> buf:Base.String.t -> pos:Base.Int.t -> len:Base.Int.t -> Base.Unit.t Base.Option.t
val really_input_exn : t -> buf:Base.String.t -> pos:Base.Int.t -> len:Base.Int.t -> Base.Unit.t

Same as Pervasives.really_input, for backwards compatibility

val input_char : t -> Base.Char.t Base.Option.t

Read one character from the given input channel. Return None if there are no more characters to read.

val input_byte : t -> Base.Int.t Base.Option.t

Same as input_char, but return the 8-bit integer representing the character. Return None if an end of file was reached.

val input_binary_int : t -> Base.Int.t Base.Option.t

Read an integer encoded in binary format (4 bytes, big-endian) from the given input channel. See Pervasives.output_binary_int. Return None if an end of file was reached while reading the integer.

val unsafe_input_value : t -> _ Base.Option.t

Ocaml's built-in marshal format

val input_buffer : t -> Base.Buffer.t -> len:Base.Int.t -> Base.Unit.t Base.Option.t

input_buffer t buf ~len reads at most len characters from the input channel t and stores them at the end of buffer buf. Return None if the channel contains fewer than len characters. In this case, the characters are still added to the buffer, so as to avoid loss of data.

val input_all : t -> Base.String.t
val input_line : ?fix_win_eol:Base.Bool.t -> t -> Base.String.t Base.Option.t

input_line ?fix_win_eol t reads a line from t and returns it, without the newline ("\n") character at the end, and, if fix_win_eol the trailing "\r\n" is dropped.

val input_line_exn : ?fix_win_eol:Base.Bool.t -> t -> Base.String.t
val fold_lines : ?fix_win_eol:Base.Bool.t -> t -> init:'a -> f:('a -> Base.String.t -> 'a) -> 'a

fold_lines ?fix_win_eol t ~init ~f folds over the lines read from t using input_line. Lines are provided to f in the order they are found in the file.

val input_lines : ?fix_win_eol:Base.Bool.t -> t -> Base.String.t Base.List.t

Completely reads an input channel and returns the results as a list of strings. Each line in one string.

val iter_lines : ?fix_win_eol:Base.Bool.t -> t -> f:(Base.String.t -> Base.Unit.t) -> Base.Unit.t

iter_lines ?fix_win_eol t ~f applies f to each line read from t using input_line.

val seek : t -> Base.Int64.t -> Base.Unit.t

This works only for regular files. On files of other kinds, the behavior is unspecified.

val pos : t -> Base.Int64.t
val length : t -> Base.Int64.t

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.

val set_binary_mode : t -> Base.Bool.t -> Base.Unit.t

same as Pervasives.set_binary_mode_in, only applicable for Windows or Cygwin, no-op otherwise

These open the filename, read all lines, and close the file.

val read_all : Base.String.t -> Base.String.t