]>
Commit | Line | Data |
---|---|---|
1 | \section{\class{wxFile}}\label{wxfile} | |
2 | ||
3 | A wxFile performs raw file I/O. This is a very small class designed to | |
4 | minimize the overhead of using it - in fact, there is hardly any overhead at | |
5 | all, but using it brings you automatic error checking and hides differences | |
6 | between platforms and compilers. wxFile also automatically closes the file in | |
7 | its destructor making it unnecessary to worry about forgetting to do it. | |
8 | wxFile is a wrapper around {\tt file descriptor.} - see also | |
9 | \helpref{wxFFile}{wxffile} for a wrapper around {\tt FILE} structure. | |
10 | ||
11 | \wxheading{Derived from} | |
12 | ||
13 | None. | |
14 | ||
15 | \wxheading{Include files} | |
16 | ||
17 | <wx/file.h> | |
18 | ||
19 | \wxheading{Constants} | |
20 | ||
21 | wx/file.h defines the following constants: | |
22 | ||
23 | \begin{verbatim} | |
24 | #define wxS_IRUSR 00400 | |
25 | #define wxS_IWUSR 00200 | |
26 | #define wxS_IXUSR 00100 | |
27 | ||
28 | #define wxS_IRGRP 00040 | |
29 | #define wxS_IWGRP 00020 | |
30 | #define wxS_IXGRP 00010 | |
31 | ||
32 | #define wxS_IROTH 00004 | |
33 | #define wxS_IWOTH 00002 | |
34 | #define wxS_IXOTH 00001 | |
35 | ||
36 | // default mode for the new files: corresponds to umask 022 | |
37 | #define wxS_DEFAULT (wxS_IRUSR | wxS_IWUSR | wxS_IRGRP | wxS_IWGRP | wxS_IROTH | wxS_IWOTH) | |
38 | \end{verbatim} | |
39 | ||
40 | These constants define the file access rights and are used with | |
41 | \helpref{wxFile::Create}{wxfilecreate} and \helpref{wxFile::Open}{wxfileopen}. | |
42 | ||
43 | The {\it OpenMode} enumeration defines the different modes for opening a file, | |
44 | it is defined inside wxFile class so its members should be specified with {\it wxFile::} scope | |
45 | resolution prefix. It is also used with \helpref{wxFile::Access}{wxfileaccess} function. | |
46 | ||
47 | \twocolwidtha{7cm} | |
48 | \begin{twocollist}\itemsep=0pt% | |
49 | \twocolitem{{\bf wxFile::read}}{Open file for reading or test if it can be opened for reading with Access()} | |
50 | \twocolitem{{\bf wxFile::write}}{Open file for writing deleting the contents of the file if it already exists | |
51 | or test if it can be opened for writing with Access()} | |
52 | \twocolitem{{\bf wxFile::read\_write}}{Open file for reading and writing; can not be used with Access()} | |
53 | \twocolitem{{\bf wxFile::write\_append}}{Open file for appending: the file is opened for writing, but the old | |
54 | contents of the file is not erased and the file pointer is initially placed at the end of the file; | |
55 | can not be used with Access(). This is the same as {\bf wxFile::write} if the | |
56 | file doesn't exist.} | |
57 | \twocolitem{{\bf wxFile::write\_excl}}{Open the file securely for writing (Uses O\_EXCL | O\_CREAT). | |
58 | Will fail if the file already exists, else create and open it atomically. Useful for opening temporary files without being vulnerable to race exploits.} | |
59 | \end{twocollist} | |
60 | ||
61 | Other constants defined elsewhere but used by wxFile functions are wxInvalidOffset which represents an | |
62 | invalid value of type {\it off\_t} and is returned by functions returning {\it off\_t} on error and the seek | |
63 | mode constants used with \helpref{Seek()}{wxfileseek}: | |
64 | ||
65 | \twocolwidtha{7cm} | |
66 | \begin{twocollist}\itemsep=0pt% | |
67 | \twocolitem{{\bf wxFromStart}}{Count offset from the start of the file} | |
68 | \twocolitem{{\bf wxFromCurrent}}{Count offset from the current position of the file pointer} | |
69 | \twocolitem{{\bf wxFromEnd}}{Count offset from the end of the file (backwards)} | |
70 | \end{twocollist} | |
71 | ||
72 | \latexignore{\rtfignore{\wxheading{Members}}} | |
73 | ||
74 | \membersection{wxFile::wxFile}\label{wxfileconstr} | |
75 | ||
76 | \func{}{wxFile}{\void} | |
77 | ||
78 | Default constructor. | |
79 | ||
80 | \func{}{wxFile}{\param{const char*}{ filename}, \param{wxFile::OpenMode}{ mode = wxFile::read}} | |
81 | ||
82 | Opens a file with the given mode. As there is no way to return whether the | |
83 | operation was successful or not from the constructor you should test the | |
84 | return value of \helpref{IsOpened}{wxfileisopened} to check that it didn't | |
85 | fail. | |
86 | ||
87 | \func{}{wxFile}{\param{int}{ fd}} | |
88 | ||
89 | Associates the file with the given file descriptor, which has already been opened. | |
90 | ||
91 | \wxheading{Parameters} | |
92 | ||
93 | \docparam{filename}{The filename.} | |
94 | ||
95 | \docparam{mode}{The mode in which to open the file. May be one of {\bf wxFile::read}, {\bf wxFile::write} and {\bf wxFile::read\_write}.} | |
96 | ||
97 | \docparam{fd}{An existing file descriptor (see \helpref{Attach()}{wxfileattach} for the list of predefined descriptors)} | |
98 | ||
99 | \membersection{wxFile::\destruct{wxFile}} | |
100 | ||
101 | \func{}{\destruct{wxFile}}{\void} | |
102 | ||
103 | Destructor will close the file. | |
104 | ||
105 | NB: it is not virtual so you should use wxFile polymorphically. | |
106 | ||
107 | \membersection{wxFile::Access}\label{wxfileaccess} | |
108 | ||
109 | \func{static bool}{Access}{\param{const char *}{ name}, \param{OpenMode}{ mode}} | |
110 | ||
111 | This function verifies if we may access the given file in specified mode. Only | |
112 | values of wxFile::read or wxFile::write really make sense here. | |
113 | ||
114 | \membersection{wxFile::Attach}\label{wxfileattach} | |
115 | ||
116 | \func{void}{Attach}{\param{int}{ fd}} | |
117 | ||
118 | Attaches an existing file descriptor to the wxFile object. Example of predefined | |
119 | file descriptors are 0, 1 and 2 which correspond to stdin, stdout and stderr (and | |
120 | have symbolic names of {\bf wxFile::fd\_stdin}, {\bf wxFile::fd\_stdout} and {\bf wxFile::fd\_stderr}). | |
121 | ||
122 | The descriptor should be already opened and it will be closed by wxFile | |
123 | object. | |
124 | ||
125 | \membersection{wxFile::Close}\label{wxfileclose} | |
126 | ||
127 | \func{void}{Close}{\void} | |
128 | ||
129 | Closes the file. | |
130 | ||
131 | \membersection{wxFile::Create}\label{wxfilecreate} | |
132 | ||
133 | \func{bool}{Create}{\param{const char*}{ filename}, \param{bool}{ overwrite = FALSE}, \param{int }{access = wxS\_DEFAULT}} | |
134 | ||
135 | Creates a file for writing. If the file already exists, setting {\bf overwrite} to TRUE | |
136 | will ensure it is overwritten. | |
137 | ||
138 | \membersection{wxFile::Detach}\label{wxfiledetach} | |
139 | ||
140 | \func{void}{Detach}{\void} | |
141 | ||
142 | Get back a file descriptor from wxFile object - the caller is responsible for closing the file if this | |
143 | descriptor is opened. \helpref{IsOpened()}{wxfileisopened} will return FALSE after call to Detach(). | |
144 | ||
145 | \membersection{wxFile::fd}\label{wxfilefd} | |
146 | ||
147 | \constfunc{int}{fd}{\void} | |
148 | ||
149 | Returns the file descriptor associated with the file. | |
150 | ||
151 | \membersection{wxFile::Eof}\label{wxfileeof} | |
152 | ||
153 | \constfunc{bool}{Eof}{\void} | |
154 | ||
155 | Returns TRUE if the end of the file has been reached. | |
156 | ||
157 | Note that the behaviour of the file pointer based class | |
158 | \helpref{wxFFile}{wxffile} is different as \helpref{wxFFile::Eof}{wxffileeof} | |
159 | will return TRUE here only if an attempt has been made to read | |
160 | {\it past} the last byte of the file, while wxFile::Eof() will return TRUE | |
161 | even before such attempt is made if the file pointer is at the last position | |
162 | in the file. | |
163 | ||
164 | Note also that this function doesn't work on unseekable file descriptors | |
165 | (examples include pipes, terminals and sockets under Unix) and an attempt to | |
166 | use it will result in an error message in such case. So, to read the entire | |
167 | file into memory, you should write a loop which uses | |
168 | \helpref{Read}{wxfileread} repeatedly and tests its return condition instead | |
169 | of using Eof() as this will not work for special files under Unix. | |
170 | ||
171 | \membersection{wxFile::Exists}\label{wxfileexists} | |
172 | ||
173 | \func{static bool}{Exists}{\param{const char*}{ filename}} | |
174 | ||
175 | Returns TRUE if the given name specifies an existing regular file (not a | |
176 | directory or a link) | |
177 | ||
178 | \membersection{wxFile::Flush}\label{wxfileflush} | |
179 | ||
180 | \func{bool}{Flush}{\void} | |
181 | ||
182 | Flushes the file descriptor. | |
183 | ||
184 | Note that wxFile::Flush is not implemented on some Windows compilers | |
185 | due to a missing fsync function, which reduces the usefulness of this function | |
186 | (it can still be called but it will do nothing on unsupported compilers). | |
187 | ||
188 | \membersection{wxFile::IsOpened}\label{wxfileisopened} | |
189 | ||
190 | \constfunc{bool}{IsOpened}{\void} | |
191 | ||
192 | Returns TRUE if the file has been opened. | |
193 | ||
194 | \membersection{wxFile::Length}\label{wxfilelength} | |
195 | ||
196 | \constfunc{off\_t}{Length}{\void} | |
197 | ||
198 | Returns the length of the file. | |
199 | ||
200 | \membersection{wxFile::Open}\label{wxfileopen} | |
201 | ||
202 | \func{bool}{Open}{\param{const char*}{ filename}, \param{wxFile::OpenMode}{ mode = wxFile::read}} | |
203 | ||
204 | Opens the file, returning TRUE if successful. | |
205 | ||
206 | \wxheading{Parameters} | |
207 | ||
208 | \docparam{filename}{The filename.} | |
209 | ||
210 | \docparam{mode}{The mode in which to open the file. May be one of {\bf wxFile::read}, {\bf wxFile::write} and {\bf wxFile::read\_write}.} | |
211 | ||
212 | \membersection{wxFile::Read}\label{wxfileread} | |
213 | ||
214 | \func{off\_t}{Read}{\param{void*}{ buffer}, \param{off\_t}{ count}} | |
215 | ||
216 | Reads the specified number of bytes into a buffer, returning the actual number read. | |
217 | ||
218 | \wxheading{Parameters} | |
219 | ||
220 | \docparam{buffer}{A buffer to receive the data.} | |
221 | ||
222 | \docparam{count}{The number of bytes to read.} | |
223 | ||
224 | \wxheading{Return value} | |
225 | ||
226 | The number of bytes read, or the symbol {\bf wxInvalidOffset} (-1) if there was an error. | |
227 | ||
228 | \membersection{wxFile::Seek}\label{wxfileseek} | |
229 | ||
230 | \func{off\_t}{Seek}{\param{off\_t }{ofs}, \param{wxSeekMode }{mode = wxFromStart}} | |
231 | ||
232 | Seeks to the specified position. | |
233 | ||
234 | \wxheading{Parameters} | |
235 | ||
236 | \docparam{ofs}{Offset to seek to.} | |
237 | ||
238 | \docparam{mode}{One of {\bf wxFromStart}, {\bf wxFromEnd}, {\bf wxFromCurrent}.} | |
239 | ||
240 | \wxheading{Return value} | |
241 | ||
242 | The actual offset position achieved, or wxInvalidOffset on failure. | |
243 | ||
244 | \membersection{wxFile::SeekEnd}\label{wxfileseekend} | |
245 | ||
246 | \func{off\_t}{SeekEnd}{\param{off\_t }{ofs = 0}} | |
247 | ||
248 | Moves the file pointer to the specified number of bytes before the end of the file. | |
249 | ||
250 | \wxheading{Parameters} | |
251 | ||
252 | \docparam{ofs}{Number of bytes before the end of the file.} | |
253 | ||
254 | \wxheading{Return value} | |
255 | ||
256 | The actual offset position achieved, or wxInvalidOffset on failure. | |
257 | ||
258 | \membersection{wxFile::Tell}\label{wxfiletell} | |
259 | ||
260 | \constfunc{off\_t}{Tell}{\void} | |
261 | ||
262 | Returns the current position or wxInvalidOffset if file is not opened or if another | |
263 | error occurred. | |
264 | ||
265 | \membersection{wxFile::Write}\label{wxfilewrite} | |
266 | ||
267 | \func{size\_t}{Write}{\param{const void*}{ buffer}, \param{off\_t}{ count}} | |
268 | ||
269 | Writes the specified number of bytes from a buffer. | |
270 | ||
271 | \wxheading{Parameters} | |
272 | ||
273 | \docparam{buffer}{A buffer containing the data.} | |
274 | ||
275 | \docparam{count}{The number of bytes to write.} | |
276 | ||
277 | \wxheading{Return value} | |
278 | ||
279 | the number of bytes actually written | |
280 | ||
281 | \membersection{wxFile::Write}\label{wxfilewrites} | |
282 | ||
283 | \func{bool}{Write}{\param{const wxString\& }{s}, \param{wxMBConv&}{ conv = wxConvLibc}} | |
284 | ||
285 | Writes the contents of the string to the file, returns TRUE on success. | |
286 | ||
287 | The second argument is only meaningful in Unicode build of wxWindows when | |
288 | {\it conv} is used to convert {\it s} to multibyte representation. | |
289 | ||
290 | \section{\class{wxFFile}}\label{wxffile} | |
291 | ||
292 | wxFFile implements buffered file I/O. This is a very small class designed to | |
293 | minimize the overhead of using it - in fact, there is hardly any overhead at | |
294 | all, but using it brings you automatic error checking and hides differences | |
295 | between platforms and compilers. It wraps inside it a {\tt FILE *} handle used | |
296 | by standard C IO library (also known as {\tt stdio}). | |
297 | ||
298 | \wxheading{Derived from} | |
299 | ||
300 | None. | |
301 | ||
302 | \wxheading{Include files} | |
303 | ||
304 | <wx/ffile.h> | |
305 | ||
306 | \twocolwidtha{7cm} | |
307 | \begin{twocollist}\itemsep=0pt% | |
308 | \twocolitem{{\bf wxFromStart}}{Count offset from the start of the file} | |
309 | \twocolitem{{\bf wxFromCurrent}}{Count offset from the current position of the file pointer} | |
310 | \twocolitem{{\bf wxFromEnd}}{Count offset from the end of the file (backwards)} | |
311 | \end{twocollist} | |
312 | ||
313 | \latexignore{\rtfignore{\wxheading{Members}}} | |
314 | ||
315 | \membersection{wxFFile::wxFFile}\label{wxffileconstr} | |
316 | ||
317 | \func{}{wxFFile}{\void} | |
318 | ||
319 | Default constructor. | |
320 | ||
321 | \func{}{wxFFile}{\param{const char*}{ filename}, \param{const char*}{ mode = "r"}} | |
322 | ||
323 | Opens a file with the given mode. As there is no way to return whether the | |
324 | operation was successful or not from the constructor you should test the | |
325 | return value of \helpref{IsOpened}{wxffileisopened} to check that it didn't | |
326 | fail. | |
327 | ||
328 | \func{}{wxFFile}{\param{FILE*}{ fp}} | |
329 | ||
330 | Opens a file with the given file pointer, which has already been opened. | |
331 | ||
332 | \wxheading{Parameters} | |
333 | ||
334 | \docparam{filename}{The filename.} | |
335 | ||
336 | \docparam{mode}{The mode in which to open the file using standard C strings. | |
337 | Note that you should use {\tt "b"} flag if you use binary files under Windows | |
338 | or the results might be unexpected due to automatic newline conversion done | |
339 | for the text files.} | |
340 | ||
341 | \docparam{fp}{An existing file descriptor, such as stderr.} | |
342 | ||
343 | \membersection{wxFFile::\destruct{wxFFile}} | |
344 | ||
345 | \func{}{\destruct{wxFFile}}{\void} | |
346 | ||
347 | Destructor will close the file. | |
348 | ||
349 | NB: it is not virtual so you should {\it not} derive from wxFFile! | |
350 | ||
351 | \membersection{wxFFile::Attach}\label{wxffileattach} | |
352 | ||
353 | \func{void}{Attach}{\param{FILE*}{ fp}} | |
354 | ||
355 | Attaches an existing file pointer to the wxFFile object. | |
356 | ||
357 | The descriptor should be already opened and it will be closed by wxFFile | |
358 | object. | |
359 | ||
360 | \membersection{wxFFile::Close}\label{wxffileclose} | |
361 | ||
362 | \func{bool}{Close}{\void} | |
363 | ||
364 | Closes the file and returns TRUE on success. | |
365 | ||
366 | \membersection{wxFFile::Detach}\label{wxffiledetach} | |
367 | ||
368 | \func{void}{Detach}{\void} | |
369 | ||
370 | Get back a file pointer from wxFFile object - the caller is responsible for closing the file if this | |
371 | descriptor is opened. \helpref{IsOpened()}{wxffileisopened} will return FALSE after call to Detach(). | |
372 | ||
373 | \membersection{wxFFile::fp}\label{wxffilefp} | |
374 | ||
375 | \constfunc{FILE *}{fp}{\void} | |
376 | ||
377 | Returns the file pointer associated with the file. | |
378 | ||
379 | \membersection{wxFFile::Eof}\label{wxffileeof} | |
380 | ||
381 | \constfunc{bool}{Eof}{\void} | |
382 | ||
383 | Returns TRUE if the an attempt has been made to read {\it past} | |
384 | the end of the file. | |
385 | ||
386 | Note that the behaviour of the file descriptor based class | |
387 | \helpref{wxFile}{wxfile} is different as \helpref{wxFile::Eof}{wxfileeof} | |
388 | will return TRUE here as soon as the last byte of the file has been | |
389 | read. | |
390 | ||
391 | \membersection{wxFFile::Flush}\label{wxffileflush} | |
392 | ||
393 | \func{bool}{Flush}{\void} | |
394 | ||
395 | Flushes the file and returns TRUE on success. | |
396 | ||
397 | \membersection{wxFFile::IsOpened}\label{wxffileisopened} | |
398 | ||
399 | \constfunc{bool}{IsOpened}{\void} | |
400 | ||
401 | Returns TRUE if the file has been opened. | |
402 | ||
403 | \membersection{wxFFile::Length}\label{wxffilelength} | |
404 | ||
405 | \constfunc{size\_t}{Length}{\void} | |
406 | ||
407 | Returns the length of the file. | |
408 | ||
409 | \membersection{wxFFile::Open}\label{wxffileopen} | |
410 | ||
411 | \func{bool}{Open}{\param{const char*}{ filename}, \param{const char*}{ mode = "r"}} | |
412 | ||
413 | Opens the file, returning TRUE if successful. | |
414 | ||
415 | \wxheading{Parameters} | |
416 | ||
417 | \docparam{filename}{The filename.} | |
418 | ||
419 | \docparam{mode}{The mode in which to open the file.} | |
420 | ||
421 | \membersection{wxFFile::Read}\label{wxffileread} | |
422 | ||
423 | \func{size\_t}{Read}{\param{void*}{ buffer}, \param{off\_t}{ count}} | |
424 | ||
425 | Reads the specified number of bytes into a buffer, returning the actual number read. | |
426 | ||
427 | \wxheading{Parameters} | |
428 | ||
429 | \docparam{buffer}{A buffer to receive the data.} | |
430 | ||
431 | \docparam{count}{The number of bytes to read.} | |
432 | ||
433 | \wxheading{Return value} | |
434 | ||
435 | The number of bytes read. | |
436 | ||
437 | \membersection{wxFFile::Seek}\label{wxffileseek} | |
438 | ||
439 | \func{bool}{Seek}{\param{long }{ofs}, \param{wxSeekMode }{mode = wxFromStart}} | |
440 | ||
441 | Seeks to the specified position and returs TRUE on success. | |
442 | ||
443 | \wxheading{Parameters} | |
444 | ||
445 | \docparam{ofs}{Offset to seek to.} | |
446 | ||
447 | \docparam{mode}{One of {\bf wxFromStart}, {\bf wxFromEnd}, {\bf wxFromCurrent}.} | |
448 | ||
449 | \membersection{wxFFile::SeekEnd}\label{wxffileseekend} | |
450 | ||
451 | \func{bool}{SeekEnd}{\param{long }{ofs = 0}} | |
452 | ||
453 | Moves the file pointer to the specified number of bytes before the end of the file | |
454 | and returns TRUE on success. | |
455 | ||
456 | \wxheading{Parameters} | |
457 | ||
458 | \docparam{ofs}{Number of bytes before the end of the file.} | |
459 | ||
460 | \membersection{wxFFile::Tell}\label{wxffiletell} | |
461 | ||
462 | \constfunc{size\_t}{Tell}{\void} | |
463 | ||
464 | Returns the current position. | |
465 | ||
466 | \membersection{wxFFile::Write}\label{wxffilewrite} | |
467 | ||
468 | \func{size\_t}{Write}{\param{const void*}{ buffer}, \param{size\_t}{ count}} | |
469 | ||
470 | Writes the specified number of bytes from a buffer. | |
471 | ||
472 | \wxheading{Parameters} | |
473 | ||
474 | \docparam{buffer}{A buffer containing the data.} | |
475 | ||
476 | \docparam{count}{The number of bytes to write.} | |
477 | ||
478 | \wxheading{Return value} | |
479 | ||
480 | Number of bytes written. | |
481 | ||
482 | \membersection{wxFFile::Write}\label{wxffilewrites} | |
483 | ||
484 | \func{bool}{Write}{\param{const wxString\& }{s}, \param{wxMBConv&}{ conv = wxConvLibc}} | |
485 | ||
486 | Writes the contents of the string to the file, returns TRUE on success. | |
487 | ||
488 | The second argument is only meaningful in Unicode build of wxWindows when | |
489 | {\it conv} is used to convert {\it s} to multibyte representation. | |
490 |