---
name: "In_channel"
section: "3o"
platform: "linux"
package: "ocaml"
version: "5.5.0-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/In_channel"
---

# In\_channel(3o)

Input channels.

## NAME

In\_channel - Input channels.

## Module

Module   In\_channel

## Documentation

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.

See  **In\_channel.examples** below.

**Since** 4.14

### Channels

*type t* =  **in\_channel**

The type of input channel.

*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.\
  \*)

Opening modes for  **In\_channel.open\_gen** .

*val stdin* :  **t**

The standard input for the process.

*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 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** .

*val open\_gen* :  **open\_flag list -\> int -\> string -\> t**

**open\_gen mode perm filename** 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.

*val with\_open\_bin* :  **string -\> (t -\> 'a) -\> 'a**

**with\_open\_bin fn f** opens a channel  **ic** on file  **fn** and returns  **f**\
     **ic** . After  **f** returns, either with a value or by raising an exception,  **ic** is guaranteed to be closed.

*val with\_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** ).

*val with\_open\_gen* :  **open\_flag list -\> 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** ).

*val close* :  **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.

*val close\_noerr* :  **t -\> unit**

Same as  **In\_channel.close** , but ignore all errors.

### Input

*val input\_char* :  **t -\> char option**

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

*val input\_byte* :  **t -\> int option**

Same as  **In\_channel.input\_char** , but return the 8-bit integer representing the character. Returns  **None** if the end of file was reached.

*val input\_line* :  **t -\> string option**

**input\_line ic** 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** .

*val really\_input\_string* :  **t -\> int -\> string option**

**really\_input\_string ic len** 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.

*val input\_all* :  **t -\> string**

**input\_all ic** 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.

*val input\_lines* :  **t -\> string list**

**input\_lines ic** 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

### Advanced input

*val input* :  **t -\> bytes -\> int -\> int -\> int**

**input ic buf pos len** 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.

**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 as  **In\_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 len** 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.

**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 as  **In\_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 ic** 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\_lines f init ic** computes  **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 pos** 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.

*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 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.

### 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 true** sets the channel  **ic** to 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 ic** returns whether the channel  **ic** is in binary mode (see  **In\_channel.set\_binary\_mode** ).

**Since** 5.2

*val isatty* :  **t -\> bool**

**isatty ic** is  **true** if  **ic** refers to a terminal or console window, **false** otherwise.

**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

```
