Out_channel(3)

πŸ’… Out_channel(3) πŸ’…

okay so this is the OCaml library manual page for Out_channel, bestie. We're talking output channels today, and honestly? iconic. Let's get into it πŸ’–βœ¨


πŸŽ€ NAME πŸŽ€

Out_channel β€” output channels. That's the vibe. Periodt. πŸ’…


🧚 Module 🧚

Module Out_channel
 : sig end

πŸ’– Documentation πŸ’–

Out_channel is literally all about output channels. This module provides functions for working with output channels. For example, you can use this module to write to a file or print to the terminal.

See Out_channel.examples below, bestie β€” yes, the examples are down there and they're cute. 🫢

✨ Since 4.14 ✨


πŸ¦‹ Channels πŸ¦‹

  • type t = out_channel β€” the type of output channel. That's the main character type right there. πŸ‘‘
  • type open_flag = open_flag = β€” opening modes for Out_channel.open_gen. Here's the whole iconic lineup:
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.
 *)

And in cute little bullet form, because we love a readable list:

  • 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. 🚦
  • val stdout : t β€” the standard output for the process. Your everyday main-character stream ✨
  • val stderr : t β€” the standard error output for the process. The dramatic side-eye stream πŸ’€
  • val open_bin : string -> t β€” open the named file for writing, and return a new output channel on that file, positioned at the beginning of the file. The file is truncated to zero length if it already exists. It is created if it does not already exist. 🎬
  • val open_text : string -> t β€” same as Out_channel.open_bin, but the file is opened in text mode, so that newline translation takes place during writes. On operating systems that do not distinguish between text mode and binary mode, this function behaves like Out_channel.open_bin. πŸ“
  • val open_gen : open_flag list -> int -> string -> t β€” open_gen mode perm filename opens the named file for writing, as described above. The extra argument mode specifies the opening mode. The extra argument perm specifies the file permissions, in case the file must be created. Out_channel.open_text and Out_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 oc on file fn and returns f oc. After f returns, either with a value or by raising an exception, oc is guaranteed to be closed. Safety slay πŸ’–
  • val with_open_text : string -> (t -> 'a) -> 'a β€” like Out_channel.with_open_bin, but the channel is opened in text mode (see Out_channel.open_text). πŸŽ€
  • val with_open_gen : open_flag list -> int -> string -> (t -> 'a) -> 'a β€” like Out_channel.with_open_bin, but can specify the opening mode and file permission, in case the file must be created (see Out_channel.open_gen). ✨
  • val close : t -> unit β€” close the given channel, flushing all buffered write operations. Output functions raise a Sys_error exception when they are applied to a closed output channel, except Out_channel.close and Out_channel.flush, which do nothing when applied to an already closed channel. Note that Out_channel.close may raise Sys_error if the operating system signals an error when flushing or closing. 🚨
bestie, this one can actually raise Sys_error if the OS gets messy while flushing or closing. no cap.
  • val close_noerr : t -> unit β€” same as Out_channel.close, but ignore all errors. The chill version πŸ™ˆ

🌈 Output 🌈

  • val output_char : t -> char -> unit β€” write the character on the given output channel. one char, one vibe πŸ’…
  • val output_byte : t -> int -> unit β€” write one 8-bit integer (as the single character with that code) on the given output channel. The given integer is taken modulo 256. math ate that πŸ”₯
  • val output_string : t -> string -> unit β€” write the string on the given output channel. πŸ“
  • val output_bytes : t -> bytes -> unit β€” write the byte sequence on the given output channel. bytes bestie 🧚

πŸ’œ Advanced output πŸ’œ

  • val output : t -> bytes -> int -> int -> unit β€” output oc buf pos len writes len characters from byte sequence buf, starting at offset pos, to the given output channel oc. Raises Invalid_argument if pos and len do not designate a valid range of buf. ⚠️
  • val output_substring : t -> string -> int -> int -> unit β€” same as Out_channel.output but take a string as argument instead of a byte sequence. string edition ✨
  • val output_bigarray : t -> ('a, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t -> int -> int -> unit β€” same as Out_channel.output but take the data from a bigarray. bigarray energy 🧬

✨ Since 5.2 ✨


🌸 Flushing 🌸

  • val flush : t -> unit β€” flush the buffer associated with the given output channel, performing all pending writes on that channel. Interactive programs must be careful about flushing standard output and standard error at the right time. timing is everything, bestie ⏰
  • val flush_all : unit -> unit β€” flush all open output channels; ignore errors. all of them. at once. slay 🧹

πŸŽ€ Seeking πŸŽ€

  • val seek : t -> int64 -> unit β€” seek chan pos sets the current writing position to pos for channel chan. This works only for regular files. On files of other kinds (such as terminals, pipes and sockets), the behavior is unspecified. 🚨
unspecified behavior on terminals, pipes and sockets, bestie β€” do not act surprised if it gets weird. πŸ’€
  • val pos : t -> int64 β€” return the current writing position for the given channel. Does not work on channels opened with the Open_append flag (returns unspecified results). 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 Out_channel.pos, then going back to this position using Out_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. meaningless bestie, don't trust it πŸ’€
  • val set_binary_mode : t -> bool -> unit β€” set_binary_mode oc true sets the channel oc to binary mode: no translations take place during output. set_binary_mode oc false sets the channel oc to text mode: depending on the operating system, some translations may take place during output. For instance, under Windows, end-of-lines will be translated from \n to \r\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 oc returns whether the channel oc is in binary mode (see Out_channel.set_binary_mode). binary mode check ✨

✨ Since 5.2 ✨

  • val set_buffered : t -> bool -> unit β€” set_buffered oc true sets the channel oc to buffered mode. In this mode, data output on oc will be buffered until either the internal buffer is full or the function Out_channel.flush or Out_channel.flush_all is called, at which point it will be sent to the output device. set_buffered oc false sets the channel oc to unbuffered mode. In this mode, data output on oc will be sent to the output device immediately. All channels are open in buffered mode by default. 🫧
  • val is_buffered : t -> bool β€” is_buffered oc returns whether the channel oc is buffered (see Out_channel.set_buffered). buffered check, bestie πŸŽ€
  • val isatty : t -> bool β€” isatty oc is true if oc refers to a terminal or console window, false otherwise. terminal girlie detection πŸ’»

✨ Since 5.1 ✨


🌟 Examples 🌟

Printing to the terminal:

             Out_channel.output_string Out_channel.stdout "hello, world"

Writing the contents of a file:

             let write_file file s =
               Out_channel.with_open_bin file
                 (fun oc -> Out_channel.output_string oc s))

copied exactly, extra paren and all. we don't edit the manual, we respect the manual πŸ’…βœ¨


xoxo πŸ’– this iconic page is brought to you by OCamldoc, dated 2026-06-22, and it's literally the Out_channel(3) manual. go off and write some iconic files, bestie πŸŽ€βœ¨

πŸ“¦ where this page comes from