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