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