1Stdlib.Out_channel(3) OCaml library Stdlib.Out_channel(3)
2
3
4
6 Stdlib.Out_channel - no description
7
9 Module Stdlib.Out_channel
10
12 Module Out_channel
13 : (module Stdlib__Out_channel)
14
15
16
17
18
19
20
21 type t = out_channel
22
23
24 The type of output channel.
25
26
27 type open_flag = open_flag =
28 | Open_rdonly (* open for reading.
29 *)
30 | Open_wronly (* open for writing.
31 *)
32 | Open_append (* open for appending: always write at end of file.
33 *)
34 | Open_creat (* create the file if it does not exist.
35 *)
36 | Open_trunc (* empty the file if it already exists.
37 *)
38 | Open_excl (* fail if Open_creat and the file already exists.
39 *)
40 | Open_binary (* open in binary mode (no conversion).
41 *)
42 | Open_text (* open in text mode (may perform conversions).
43 *)
44 | Open_nonblock (* open in non-blocking mode.
45 *)
46
47
48 Opening modes for Out_channel.open_gen .
49
50
51
52 val stdout : t
53
54 The standard output for the process.
55
56
57
58 val stderr : t
59
60 The standard error output for the process.
61
62
63
64 val open_bin : string -> t
65
66 Open the named file for writing, and return a new output channel on
67 that file, positioned at the beginning of the file. The file is trun‐
68 cated to zero length if it already exists. It is created if it does not
69 already exists.
70
71
72
73 val open_text : string -> t
74
75 Same as Out_channel.open_bin , but the file is opened in text mode, so
76 that newline translation takes place during writes. On operating sys‐
77 tems that do not distinguish between text mode and binary mode, this
78 function behaves like Out_channel.open_bin .
79
80
81
82 val open_gen : open_flag list -> int -> string -> t
83
84
85 open_gen mode perm filename opens the named file for writing, as de‐
86 scribed above. The extra argument mode specifies the opening mode. The
87 extra argument perm specifies the file permissions, in case the file
88 must be created. Out_channel.open_text and Out_channel.open_bin are
89 special cases of this function.
90
91
92
93 val with_open_bin : string -> (t -> 'a) -> 'a
94
95
96 with_open_bin fn f opens a channel oc on file fn and returns f
97 oc . After f returns, either with a value or by raising an excep‐
98 tion, oc is guaranteed to be closed.
99
100
101
102 val with_open_text : string -> (t -> 'a) -> 'a
103
104 Like Out_channel.with_open_bin , but the channel is opened in text mode
105 (see Out_channel.open_text ).
106
107
108
109 val with_open_gen : open_flag list -> int -> string -> (t -> 'a) -> 'a
110
111 Like Out_channel.with_open_bin , but can specify the opening mode and
112 file permission, in case the file must be created (see Out_chan‐
113 nel.open_gen ).
114
115
116
117 val seek : t -> int64 -> unit
118
119
120 seek chan pos sets the current writing position to pos for channel chan
121 . This works only for regular files. On files of other kinds (such as
122 terminals, pipes and sockets), the behavior is unspecified.
123
124
125
126 val pos : t -> int64
127
128 Return the current writing position for the given channel. Does not
129 work on channels opened with the Open_append flag (returns unspecified
130 results).
131
132 For files opened in text mode under Windows, the returned position is
133 approximate (owing to end-of-line conversion); in particular, saving
134 the current position with Out_channel.pos , then going back to this po‐
135 sition using Out_channel.seek will not work. For this programming id‐
136 iom to work reliably and portably, the file must be opened in binary
137 mode.
138
139
140
141 val length : t -> int64
142
143 Return the size (number of characters) of the regular file on which the
144 given channel is opened. If the channel is opened on a file that is
145 not a regular file, the result is meaningless.
146
147
148
149 val close : t -> unit
150
151 Close the given channel, flushing all buffered write operations. Out‐
152 put functions raise a Sys_error exception when they are applied to a
153 closed output channel, except Out_channel.close and Out_channel.flush ,
154 which do nothing when applied to an already closed channel. Note that
155 Out_channel.close may raise Sys_error if the operating system signals
156 an error when flushing or closing.
157
158
159
160 val close_noerr : t -> unit
161
162 Same as Out_channel.close , but ignore all errors.
163
164
165
166 val flush : t -> unit
167
168 Flush the buffer associated with the given output channel, performing
169 all pending writes on that channel. Interactive programs must be care‐
170 ful about flushing standard output and standard error at the right
171 time.
172
173
174
175 val flush_all : unit -> unit
176
177 Flush all open output channels; ignore errors.
178
179
180
181 val output_char : t -> char -> unit
182
183 Write the character on the given output channel.
184
185
186
187 val output_byte : t -> int -> unit
188
189 Write one 8-bit integer (as the single character with that code) on the
190 given output channel. The given integer is taken modulo 256.
191
192
193
194 val output_string : t -> string -> unit
195
196 Write the string on the given output channel.
197
198
199
200 val output_bytes : t -> bytes -> unit
201
202 Write the byte sequence on the given output channel.
203
204
205
206 val output : t -> bytes -> int -> int -> unit
207
208
209 output oc buf pos len writes len characters from byte sequence buf ,
210 starting at offset pos , to the given output channel oc .
211
212
213 Raises Invalid_argument if pos and len do not designate a valid range
214 of buf .
215
216
217
218 val output_substring : t -> string -> int -> int -> unit
219
220 Same as Out_channel.output but take a string as argument instead of a
221 byte sequence.
222
223
224
225 val set_binary_mode : t -> bool -> unit
226
227
228 set_binary_mode oc true sets the channel oc to binary mode: no transla‐
229 tions take place during output.
230
231
232 set_binary_mode oc false sets the channel oc to text mode: depending on
233 the operating system, some translations may take place during output.
234 For instance, under Windows, end-of-lines will be translated from \n to
235 \r\n .
236
237 This function has no effect under operating systems that do not distin‐
238 guish between text mode and binary mode.
239
240
241
242 val set_buffered : t -> bool -> unit
243
244
245 set_buffered oc true sets the channel oc to buffered mode. In this
246 mode, data output on oc will be buffered until either the internal buf‐
247 fer is full or the function Out_channel.flush or Out_channel.flush_all
248 is called, at which point it will be sent to the output device.
249
250
251 set_buffered oc false sets the channel oc to unbuffered mode. In this
252 mode, data output on oc will be sent to the output device immediately.
253
254 All channels are open in buffered mode by default.
255
256
257
258 val is_buffered : t -> bool
259
260
261 is_buffered oc returns whether the channel oc is buffered (see
262 Out_channel.set_buffered ).
263
264
265
266
267
268OCamldoc 2022-07-22 Stdlib.Out_channel(3)