In_channel(3)
π In_channel(3) β the OCaml libraryβs input-channel era π
okay so this is the OCaml library manual page for In_channel, and itβs literally your bestie for input channels. Itβs giving βread files, grab terminal input, vibe check your data.β We are covering every type, every val, every lil caveat. No skips, no cap. π«Ά
β¨ NAME β¨
In_channel β input channels. Thatβs the name, thatβs the game. π
π MODULE π
Itβs Module In_channel, bestie. Thatβs the module name.
πΈ DOCUMENTATION πΈ
Hereβs the signature vibe, exactly:
Module In_channel
: sig end
Input channels. This module provides functions for working with input channels. For example, you can use this module to read files or get user input from the terminal.
Peep In_channel.examples below.
Since 4.14 β iconic since then. β¨
π¦ CHANNELS π¦
type t = in_channelβ the type of input channel.type open_flag = open_flag =β bestie, here are the opening modes forIn_channel.open_gen:
type open_flag = open_flag =
| Open_rdonly (* open for reading.
*)
| Open_wronly (* open for writing.
*)
| Open_append (* open for appending: always write at end of file.
*)
| Open_creat (* create the file if it does not exist.
*)
| Open_trunc (* empty the file if it already exists.
*)
| Open_excl (* fail if Open_creat and the file already exists.
*)
| Open_binary (* open in binary mode (no conversion).
*)
| Open_text (* open in text mode (may perform conversions).
*)
| Open_nonblock (* open in non-blocking mode.
*)
Translation for the group chat:
Open_rdonlyβ open for reading.Open_wronlyβ open for writing.Open_appendβ open for appending: always write at end of file.Open_creatβ create the file if it does not exist.Open_truncβ empty the file if it already exists.Open_exclβ fail ifOpen_creatand the file already exists.Open_binaryβ open in binary mode (no conversion).Open_textβ open in text mode (may perform conversions).Open_nonblockβ open in non-blocking mode.
These are the opening modes for In_channel.open_gen.
val stdin : tβ the standard input for the process. Your main-character input stream. πval open_bin : string -> tβ open the named file for reading, and return a new input channel on that file, positioned at the beginning of the file.val open_text : string -> tβ same asIn_channel.open_bin, 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 likeIn_channel.open_bin.val open_gen : open_flag list -> int -> string -> tβopen_gen mode perm filenameopens the named file for reading, as described above. The extra argumentsmodeandpermspecify the opening mode and file permissions.In_channel.open_textandIn_channel.open_binare special cases of this function.val with_open_bin : string -> (t -> 'a) -> 'aβwith_open_bin fn fopens a channelicon filefnand returnsf ic. Afterfreturns, either with a value or by raising an exception,icis guaranteed to be closed. Cleanup queen behavior. π§val with_open_text : string -> (t -> 'a) -> 'aβ likeIn_channel.with_open_bin, but the channel is opened in text mode (seeIn_channel.open_text).val with_open_gen : open_flag list -> int -> string -> (t -> 'a) -> 'aβ likeIn_channel.with_open_bin, but can specify the opening mode and file permission, in case the file must be created (seeIn_channel.open_gen).val close : t -> unitβ close the given channel. Input functions raise aSys_errorexception when they are applied to a closed input channel, exceptIn_channel.close, which does nothing when applied to an already closed channel.val close_noerr : t -> unitβ same asIn_channel.close, but ignore all errors.
π INPUT π
val input_char : t -> char optionβ read one character from the given input channel. ReturnsNoneif there are no more characters to read.val input_byte : t -> int optionβ same asIn_channel.input_char, but return the 8-bit integer representing the character. ReturnsNoneif the end of file was reached.val input_line : t -> string optionβinput_line icreads characters fromicuntil a newline or the end of file is reached. Returns the string of all characters read, without the newline (if any). ReturnsNoneif the end of the file has been reached. In particular, this will be the case if the last line of input is empty.
A newline is the character \n unless the file is open in text mode and Sys.win32 is true in which case it is the sequence of characters \r\n.
val really_input_string : t -> int -> string optionβreally_input_string ic lenreadslencharacters from channelicand returns them in a new string. ReturnsNoneif the end of file is reached beforelencharacters have been read.
If the same channel is read concurrently by multiple threads, the returned string is not guaranteed to contain contiguous characters from the input.
val input_all : t -> stringβinput_all icreads all remaining data fromic.
If the same channel is read concurrently by multiple threads, the returned string is not guaranteed to contain contiguous characters from the input.
val input_lines : t -> string listβinput_lines icreads lines usingIn_channel.input_lineuntil the end of file is reached. It returns the list of all lines read, in the order they were read. The newline characters that terminate lines are not included in the returned strings. Empty lines produce empty strings.
Since 5.1
π₯ ADVANCED INPUT π₯
val input : t -> bytes -> int -> int -> intβinput ic buf pos lenreads up tolencharacters from the given channelic, storing them in byte sequencebuf, starting at character numberpos. It returns the actual number of characters read, between0andlen(inclusive). A return value of0means that the end of file was reached.
Use In_channel.really_input to read exactly len characters.
Raises Invalid_argument if pos and len do not designate a valid range of buf.
val input_bigarray : t -> ('a, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t -> int -> int -> intβ same asIn_channel.input, but read the data into a bigarray.
Since 5.2
val really_input : t -> bytes -> int -> int -> unit optionβreally_input ic buf pos lenreadslencharacters from channelic, storing them in byte sequencebuf, starting at character numberpos.
Returns None if the end of file is reached before len characters have been read.
If the same channel is read concurrently by multiple threads, the bytes read by really_input are not guaranteed to be contiguous.
Raises Invalid_argument if pos and len do not designate a valid range of buf.
val really_input_bigarray : t -> ('a, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t -> int -> int -> unit optionβ same asIn_channel.really_input, but read the data into a bigarray.
Since 5.2
val fold_lines : ('acc -> string -> 'acc) -> 'acc -> t -> 'accβfold_lines f init icreads lines fromicusingIn_channel.input_lineuntil the end of file is reached, and successively passes each line to functionfin the style of a fold. More precisely, if linesl1, ..., lNare read,fold_lines f init iccomputes:
f (... (f (f init l1) l2) ...) lN
If f has no side effects, this is equivalent to List.fold_left f init (In_channel.input_lines ic), but is more efficient since it does not construct the list of all lines read.
Since 5.1
π SEEKING π
val seek : t -> int64 -> unitβseek chan possets the current reading position toposfor channelchan. This works only for regular files. On files of other kinds, the behavior is unspecified. πval pos : t -> int64β 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 withIn_channel.pos, then going back to this position usingIn_channel.seekwill not work. For this programming idiom to work reliably and portably, the file must be opened in binary mode.
π ATTRIBUTES π
val length : t -> int64β 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 -> bool -> unitβset_binary_mode ic truesets the channelicto binary mode: no translations take place during input.
set_binary_mode ic false sets the channel ic 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 \r\n to \n.
This function has no effect under operating systems that do not distinguish between text mode and binary mode.
val is_binary_mode : t -> boolβis_binary_mode icreturns whether the channelicis in binary mode (seeIn_channel.set_binary_mode).
Since 5.2
val isatty : t -> boolβisatty icistrueificrefers to a terminal or console window,falseotherwise.
Since 5.1
π EXAMPLES π
Reading the contents of a file:
let read_file file = In_channel.with_open_bin file In_channel.input_all
Reading a line from stdin:
let user_input () = In_channel.input_line In_channel.stdin
and thatβs the whole page, bestie. This little gem comes from OCamldoc, dated 2026-06-22, under the In_channel(3) manual name. Go read something iconic. πβ¨π
π¦ where this page comes from
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms