1Stdlib.In_channel(3) OCaml library Stdlib.In_channel(3)
2
3
4
6 Stdlib.In_channel - no description
7
9 Module Stdlib.In_channel
10
12 Module In_channel
13 : (module Stdlib__In_channel)
14
15
16
17
18
19
20
21 type t = in_channel
22
23
24 The type of input 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 In_channel.open_gen .
49
50
51
52 val stdin : t
53
54 The standard input for the process.
55
56
57
58 val open_bin : string -> t
59
60 Open the named file for reading, and return a new input channel on that
61 file, positioned at the beginning of the file.
62
63
64
65 val open_text : string -> t
66
67 Same as In_channel.open_bin , but the file is opened in text mode, so
68 that newline translation takes place during reads. On operating systems
69 that do not distinguish between text mode and binary mode, this func‐
70 tion behaves like In_channel.open_bin .
71
72
73
74 val open_gen : open_flag list -> int -> string -> t
75
76
77 open_gen mode perm filename opens the named file for reading, as de‐
78 scribed above. The extra arguments mode and perm specify the opening
79 mode and file permissions. In_channel.open_text and In_chan‐
80 nel.open_bin are special cases of this function.
81
82
83
84 val with_open_bin : string -> (t -> 'a) -> 'a
85
86
87 with_open_bin fn f opens a channel ic on file fn and returns f
88 ic . After f returns, either with a value or by raising an excep‐
89 tion, ic is guaranteed to be closed.
90
91
92
93 val with_open_text : string -> (t -> 'a) -> 'a
94
95 Like In_channel.with_open_bin , but the channel is opened in text mode
96 (see In_channel.open_text ).
97
98
99
100 val with_open_gen : open_flag list -> int -> string -> (t -> 'a) -> 'a
101
102 Like In_channel.with_open_bin , but can specify the opening mode and
103 file permission, in case the file must be created (see In_chan‐
104 nel.open_gen ).
105
106
107
108 val seek : t -> int64 -> unit
109
110
111 seek chan pos sets the current reading position to pos for channel chan
112 . This works only for regular files. On files of other kinds, the be‐
113 havior is unspecified.
114
115
116
117 val pos : t -> int64
118
119 Return the current reading position for the given channel. For files
120 opened in text mode under Windows, the returned position is approximate
121 (owing to end-of-line conversion); in particular, saving the current
122 position with In_channel.pos , then going back to this position using
123 In_channel.seek will not work. For this programming idiom to work re‐
124 liably and portably, the file must be opened in binary mode.
125
126
127
128 val length : t -> int64
129
130 Return the size (number of characters) of the regular file on which the
131 given channel is opened. If the channel is opened on a file that is
132 not a regular file, the result is meaningless. The returned size does
133 not take into account the end-of-line translations that can be per‐
134 formed when reading from a channel opened in text mode.
135
136
137
138 val close : t -> unit
139
140 Close the given channel. Input functions raise a Sys_error exception
141 when they are applied to a closed input channel, except In_chan‐
142 nel.close , which does nothing when applied to an already closed chan‐
143 nel.
144
145
146
147 val close_noerr : t -> unit
148
149 Same as In_channel.close , but ignore all errors.
150
151
152
153 val input_char : t -> char option
154
155 Read one character from the given input channel. Returns None if there
156 are no more characters to read.
157
158
159
160 val input_byte : t -> int option
161
162 Same as In_channel.input_char , but return the 8-bit integer represent‐
163 ing the character. Returns None if the end of file was reached.
164
165
166
167 val input_line : t -> string option
168
169
170 input_line ic reads characters from ic until a newline or the end of
171 file is reached. Returns the string of all characters read, without
172 the newline (if any). Returns None if the end of the file has been
173 reached. In particular, this will be the case if the last line of in‐
174 put is empty.
175
176 A newline is the character \n unless the file is open in text mode and
177 Sys.win32 is true in which case it is the sequence of characters \r\n .
178
179
180
181 val input : t -> bytes -> int -> int -> int
182
183
184 input ic buf pos len reads up to len characters from the given channel
185 ic , storing them in byte sequence buf , starting at character number
186 pos . It returns the actual number of characters read, between 0 and
187 len (inclusive). A return value of 0 means that the end of file was
188 reached.
189
190 Use In_channel.really_input to read exactly len characters.
191
192
193 Raises Invalid_argument if pos and len do not designate a valid range
194 of buf .
195
196
197
198 val really_input : t -> bytes -> int -> int -> unit option
199
200
201 really_input ic buf pos len reads len characters from channel ic ,
202 storing them in byte sequence buf , starting at character number pos .
203
204 Returns None if the end of file is reached before len characters have
205 been read.
206
207
208 Raises Invalid_argument if pos and len do not designate a valid range
209 of buf .
210
211
212
213 val really_input_string : t -> int -> string option
214
215
216 really_input_string ic len reads len characters from channel ic and re‐
217 turns them in a new string. Returns None if the end of file is reached
218 before len characters have been read.
219
220
221
222 val input_all : t -> string
223
224
225 input_all ic reads all remaining data from ic .
226
227
228
229 val set_binary_mode : t -> bool -> unit
230
231
232 set_binary_mode ic true sets the channel ic to binary mode: no transla‐
233 tions take place during input.
234
235
236 set_binary_mode ic false sets the channel ic to text mode: depending on
237 the operating system, some translations may take place during input.
238 For instance, under Windows, end-of-lines will be translated from \r\n
239 to \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
247
248OCamldoc 2022-07-22 Stdlib.In_channel(3)