Stdlib.Out_channel(3)
okay so bestie, this is Stdlib.Out_channel(3) from the OCaml library manual β weβre doing output channels today and honestly itβs giving essential π
β¨
π NAME π
Stdlib.Out_channel - no description π thatβs literally the whole NAME section. No cap.
π§© Module π§©
Module Stdlib.Out_channel
π Documentation π
Module Out_channel
: (module Stdlib__Out_channel)
π¦ Channels π¦
type t = out_channel
This is the type of output channel, periodt.
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.
*)
These are the opening modes for Out_channel.open_gen.
val stdout : tβ the standard output for the process. main-character energy goes here β¨val stderr : tβ the standard error output for the process. drama goes here π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 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. guaranteed close is the self-care we deserve π«Ά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.
warning bestie:Out_channel.closecan raiseSys_errorif the OS signals an error while flushing or closing. Not me ignoring errors π
val close_noerr : t -> unitβ same asOut_channel.close, but ignore all errors.
π Output π
val output_char : t -> char -> unitβ write the character on the given output channel.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.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.
π₯ 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.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. 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.val flush_all : unit -> unitβ flush all open output channels; ignore errors.
π― 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.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.
iykyk: if you wantpos+seekto be reliable and portable, open the file in binary mode. No cap.
π 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.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). Since 5.2val 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).val isatty : t -> boolβisatty ocistrueifocrefers to a terminal or console window,falseotherwise. 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))
copy that last one exactly, extra closing paren and all β yes bestie, itβs in the original ππ
mwah π this page was brought to you by OCamldoc, dated 2026-06-22, for Stdlib.Out_channel(3) β now go flush your buffers and slay β¨π
π¦ 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