Module In_channel
: (moduleStdlib__In_channel)Channelstypet = in_channel
The type of input channel.
typeopen_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.
*)
Opening modes for In_channel.open_gen .
valstdin : t
The standard input for the process.
valopen_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.
valopen_text : string->t
Same as In_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 like In_channel.open_bin .
valopen_gen : open_flaglist->int->string->topen_genmodepermfilename opens the named file for reading, as described above. The extra arguments
mode and perm specify the opening mode and file permissions. In_channel.open_text and
In_channel.open_bin are special cases of this function.
valwith_open_bin : string->(t->'a)->'awith_open_binfnf opens a channel ic on file fn and returns fic . After f returns, either with a value or by raising an exception, ic is guaranteed to be closed.
valwith_open_text : string->(t->'a)->'a
Like In_channel.with_open_bin , but the channel is opened in text mode (see In_channel.open_text ).
valwith_open_gen : open_flaglist->int->string->(t->'a)->'a
Like In_channel.with_open_bin , but can specify the opening mode and file permission, in case the file
must be created (see In_channel.open_gen ).
valclose : t->unit
Close the given channel. Input functions raise a Sys_error exception when they are applied to a closed
input channel, except In_channel.close , which does nothing when applied to an already closed channel.
valclose_noerr : t->unit
Same as In_channel.close , but ignore all errors.
Inputvalinput_char : t->charoption
Read one character from the given input channel. Returns None if there are no more characters to read.
valinput_byte : t->intoption
Same as In_channel.input_char , but return the 8-bit integer representing the character. Returns None if
the end of file was reached.
valinput_line : t->stringoptioninput_lineic reads characters from ic until a newline or the end of file is reached. Returns the string
of all characters read, without the newline (if any). Returns None if 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 .
valreally_input_string : t->int->stringoptionreally_input_stringiclen reads len characters from channel ic and returns them in a new string.
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 returned string is not guaranteed to
contain contiguous characters from the input.
valinput_all : t->stringinput_allic reads all remaining data from ic .
If the same channel is read concurrently by multiple threads, the returned string is not guaranteed to
contain contiguous characters from the input.
valinput_lines : t->stringlistinput_linesic reads lines using In_channel.input_line until 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
Advancedinputvalinput : t->bytes->int->int->intinputicbufposlen reads up to len characters from the given channel ic , storing them in byte sequence
buf , starting at character number pos . It returns the actual number of characters read, between 0 and
len (inclusive). A return value of 0 means that the end of file was reached.
Use In_channel.really_input to read exactly len characters.
RaisesInvalid_argument if pos and len do not designate a valid range of buf .
valinput_bigarray : t->('a,Bigarray.int8_unsigned_elt,Bigarray.c_layout)Bigarray.Array1.t->int->int->int
Same as In_channel.input , but read the data into a bigarray.
Since 5.2
valreally_input : t->bytes->int->int->unitoptionreally_inputicbufposlen reads len characters from channel ic , storing them in byte sequence buf ,
starting at character number pos .
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.
RaisesInvalid_argument if pos and len do not designate a valid range of buf .
valreally_input_bigarray : t->('a,Bigarray.int8_unsigned_elt,Bigarray.c_layout)Bigarray.Array1.t->int->int->unitoption
Same as In_channel.really_input , but read the data into a bigarray.
Since 5.2
valfold_lines : ('acc->string->'acc)->'acc->t->'accfold_linesfinitic reads lines from ic using In_channel.input_line until the end of file is reached,
and successively passes each line to function f in the style of a fold. More precisely, if lines l1,...,lN are read, fold_linesfinitic computes f(...(f(finitl1)l2)...)lN . If f has no side
effects, this is equivalent to List.fold_leftfinit(In_channel.input_linesic) , but is more efficient
since it does not construct the list of all lines read.
Since 5.1
Seekingvalseek : t->int64->unitseekchanpos sets the current reading position to pos for channel chan . This works only for regular
files. On files of other kinds, the behavior is unspecified.
valpos : 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 with In_channel.pos , then going back to this position using In_channel.seek will not work. For
this programming idiom to work reliably and portably, the file must be opened in binary mode.
Attributesvallength : 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.
valset_binary_mode : t->bool->unitset_binary_modeictrue sets the channel ic to binary mode: no translations take place during input.
set_binary_modeicfalse 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.
valis_binary_mode : t->boolis_binary_modeic returns whether the channel ic is in binary mode (see In_channel.set_binary_mode ).
Since 5.2
valisatty : t->boolisattyic is true if ic refers to a terminal or console window, false otherwise.
Since 5.1
Examples
Reading the contents of a file:
letread_filefile=In_channel.with_open_binfileIn_channel.input_all
Reading a line from stdin:
letuser_input()=In_channel.input_lineIn_channel.stdin
OCamldoc 2025-06-12 Stdlib.In_channel(3o)