Module Stdio.Out_channel
An output channel for doing blocking writes to destinations like files and sockets.
Note that an Out_channel.t is a custom block with a finalizer, and so is allocated directly to the major heap. Creating a lot of out_channels can result in many major collections and poor performance.
Note that this is simply another interface on the out_channel type in the OCaml standard library.
As for the output functions in the standard library, all the functions in this module, unless otherwise specified, can raise Sys_error when the system calls they invoke fail.
type t= Caml.out_channel
val sexp_of_t : t -> Base.Sexp.t
type 'a with_create_args= ?binary:Base.bool -> ?append:Base.bool -> ?fail_if_exists:Base.bool -> ?perm:Base.int -> 'a
val create : (Base.string -> t) with_create_argsval with_file : (Base.string -> f:(t -> 'a) -> 'a) with_create_argsval close : t -> Base.unitclose tflushes and closest, and may raise an exception.closereturns () and does not raise iftis already closed.closeraises an exception if the close() system call on the underlying file descriptor fails (i.e. returns -1), which would happen in the following cases:EBADF -- this would happen if someone else did close() system call on the underlying fd, which I would think a rare event.
EINTR -- would happen if the system call was interrupted by a signal, which would be rare. Also, I think we should probably just catch EINTR and re-attempt the close. Unfortunately, we can't do that in OCaml because the OCaml library marks the out_channel as closed even if the close syscall fails, so a subsequent call
close_out_channelwill be a no-op. This should really be fixed in the OCaml library C code, having it restart the close() syscall on EINTR. I put a couple CRs infixed_close_channel, our rework of OCaml'scaml_ml_close_channel,EIO -- I don't recall seeing this. I think it's rare.
See "man 2 close" for details.
val set_binary_mode : t -> Base.bool -> Base.unitval flush : t -> Base.unitval output : t -> buf:Base.bytes -> pos:Base.int -> len:Base.int -> Base.unitval output_string : t -> Base.string -> Base.unitval output_substring : t -> buf:Base.string -> pos:Base.int -> len:Base.int -> Base.unitval output_bytes : t -> Base.Bytes.t -> Base.unitval output_char : t -> Base.char -> Base.unitval output_byte : t -> Base.int -> Base.unitval output_binary_int : t -> Base.int -> Base.unitval output_buffer : t -> Base.Buffer.t -> Base.unitval output_value : t -> _ -> Base.unitOCaml's internal Marshal format
val newline : t -> Base.unitval output_lines : t -> Base.string Base.list -> Base.unitOutputs a list of lines, each terminated by a newline character
val fprintf : t -> ('a, t, Base.unit) Base.format -> 'aFormatted printing to an out channel. This is the same as
Printf.sprintfexcept that it outputs totinstead of returning a string. Similarly, the function arguments corresponding to conversions specifications such as%aor%ttakestas argument and must print to it instead of returning a string.
val printf : ('a, t, Base.unit) Base.format -> 'aprintf fmtis the same asfprintf stdout fmt
val print_s : ?mach:Base.unit -> Base.Sexp.t -> Base.unitprint_s sexpoutputssexponstdout, by default usingSexp.to_string_hum, or, with~mach:(),Sexp.to_string_mach.
val eprint_s : ?mach:Base.unit -> Base.Sexp.t -> Base.uniteprint_s sexpoutputssexponstderr, by default usingSexp.to_string_hum, or, with~mach:(),Sexp.to_string_mach.
val eprintf : ('a, t, Base.unit) Base.format -> 'aeprintf fmtis the same asfprintf stderr fmt
val kfprintf : (t -> 'a) -> t -> ('b, t, Base.unit, 'a) Base.format4 -> 'bkfprintf k t fmtis the same asfprintf t fmt, but instead of returning immediately, passes the out channel tokat the end of printing.
val print_string : Base.string -> Base.unitprint_string s=output_string stdout s
val print_endline : Base.string -> Base.unitprint_endline stroutputsstrtostdoutfollowed by a newline then flushesstdout
val prerr_endline : Base.string -> Base.unitprerr_endline stroutputsstrtostderrfollowed by a newline then flushesstderr
val seek : t -> Base.int64 -> Base.unitval pos : t -> Base.int64val length : t -> Base.int64val write_lines : Base.string -> Base.string Base.list -> Base.unitThe first argument of these is the file name to write to.
val write_all : Base.string -> data:Base.string -> Base.unit