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 forOut_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 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. π¦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 asOut_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 likeOut_channel.open_bin. πval open_gen : open_flag list -> int -> string -> tβopen_gen mode perm filenameopens the named file for writing, as described above. The extra argumentmodespecifies the opening mode. The extra argumentpermspecifies the file permissions, in case the file must be created.Out_channel.open_textandOut_channel.open_binare special cases of this function. π οΈval with_open_bin : string -> (t -> 'a) -> 'aβwith_open_bin fn fopens a channelocon filefnand returnsf oc. Afterfreturns, either with a value or by raising an exception,ocis guaranteed to be closed. Safety slay πval with_open_text : string -> (t -> 'a) -> 'aβ likeOut_channel.with_open_bin, but the channel is opened in text mode (seeOut_channel.open_text). πval with_open_gen : open_flag list -> int -> string -> (t -> 'a) -> 'aβ likeOut_channel.with_open_bin, but can specify the opening mode and file permission, in case the file must be created (seeOut_channel.open_gen). β¨val close : t -> unitβ close the given channel, flushing all buffered write operations. Output functions raise aSys_errorexception when they are applied to a closed output channel, exceptOut_channel.closeandOut_channel.flush, which do nothing when applied to an already closed channel. Note thatOut_channel.closemay raiseSys_errorif 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 asOut_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 lenwriteslencharacters from byte sequencebuf, starting at offsetpos, to the given output channeloc. RaisesInvalid_argumentifposandlendo not designate a valid range ofbuf. β οΈval output_substring : t -> string -> int -> int -> unitβ same asOut_channel.outputbut 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 asOut_channel.outputbut 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 possets the current writing position toposfor channelchan. 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 theOpen_appendflag (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 withOut_channel.pos, then going back to this position usingOut_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. meaningless bestie, don't trust it πval set_binary_mode : t -> bool -> unitβset_binary_mode oc truesets the channelocto binary mode: no translations take place during output.set_binary_mode oc falsesets the channelocto 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\nto\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 ocreturns whether the channelocis in binary mode (seeOut_channel.set_binary_mode). binary mode check β¨
β¨ Since 5.2 β¨
val set_buffered : t -> bool -> unitβset_buffered oc truesets the channelocto buffered mode. In this mode, data output onocwill be buffered until either the internal buffer is full or the functionOut_channel.flushorOut_channel.flush_allis called, at which point it will be sent to the output device.set_buffered oc falsesets the channelocto unbuffered mode. In this mode, data output onocwill be sent to the output device immediately. All channels are open in buffered mode by default. π«§val is_buffered : t -> boolβis_buffered ocreturns whether the channelocis buffered (seeOut_channel.set_buffered). buffered check, bestie πval isatty : t -> boolβisatty ocistrueifocrefers to a terminal or console window,falseotherwise. 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
- 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