]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/file.tex
SetDefaultStyle(wxTextAttr()) now resets the default style instead of not changing...
[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.}
f1104df1
RL
57\twocolitem{{\bf wxFile::write\_excl}}{Open the file securely for writing (Uses O\_EXCL | O\_CREAT).
58Will fail if the file already exists, else create and open it atomically. Useful for opening temporary files without being vulnerable to race exploits.}
247aba10
VZ
59\end{twocollist}
60
61Other constants defined elsewhere but used by wxFile functions are wxInvalidOffset which represents an
62invalid value of type {\it off\_t} and is returned by functions returning {\it off\_t} on error and the seek
63mode constants used with \helpref{Seek()}{wxfileseek}:
64
65\twocolwidtha{7cm}
6be663cf 66\begin{twocollist}\itemsep=0pt%
247aba10
VZ
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
a660d684
KB
72\latexignore{\rtfignore{\wxheading{Members}}}
73
74\membersection{wxFile::wxFile}\label{wxfileconstr}
75
76\func{}{wxFile}{\void}
77
78Default constructor.
79
80\func{}{wxFile}{\param{const char*}{ filename}, \param{wxFile::OpenMode}{ mode = wxFile::read}}
81
247aba10
VZ
82Opens a file with the given mode. As there is no way to return whether the
83operation was successful or not from the constructor you should test the
84return value of \helpref{IsOpened}{wxfileisopened} to check that it didn't
85fail.
a660d684
KB
86
87\func{}{wxFile}{\param{int}{ fd}}
88
e694c22c 89Associates the file with the given file descriptor, which has already been opened.
a660d684
KB
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
247aba10 97\docparam{fd}{An existing file descriptor (see \helpref{Attach()}{wxfileattach} for the list of predefined descriptors)}
a660d684
KB
98
99\membersection{wxFile::\destruct{wxFile}}
100
101\func{}{\destruct{wxFile}}{\void}
102
247aba10 103Destructor will close the file.
a660d684 104
e694c22c 105NB: it is not virtual so you should use wxFile polymorphically.
247aba10
VZ
106
107\membersection{wxFile::Access}\label{wxfileaccess}
6be663cf
JS
108
109\func{static bool}{Access}{\param{const char *}{ name}, \param{OpenMode}{ mode}}
a660d684 110
247aba10
VZ
111This function verifies if we may access the given file in specified mode. Only
112values of wxFile::read or wxFile::write really make sense here.
113
114\membersection{wxFile::Attach}\label{wxfileattach}
6be663cf 115
a660d684
KB
116\func{void}{Attach}{\param{int}{ fd}}
117
247aba10
VZ
118Attaches an existing file descriptor to the wxFile object. Example of predefined
119file descriptors are 0, 1 and 2 which correspond to stdin, stdout and stderr (and
28c9c76e 120have symbolic names of {\bf wxFile::fd\_stdin}, {\bf wxFile::fd\_stdout} and {\bf wxFile::fd\_stderr}).
247aba10
VZ
121
122The descriptor should be already opened and it will be closed by wxFile
123object.
a660d684
KB
124
125\membersection{wxFile::Close}\label{wxfileclose}
126
127\func{void}{Close}{\void}
128
129Closes the file.
130
131\membersection{wxFile::Create}\label{wxfilecreate}
132
247aba10 133\func{bool}{Create}{\param{const char*}{ filename}, \param{bool}{ overwrite = FALSE}, \param{int }{access = wxS\_DEFAULT}}
a660d684
KB
134
135Creates a file for writing. If the file already exists, setting {\bf overwrite} to TRUE
136will ensure it is overwritten.
137
247aba10 138\membersection{wxFile::Detach}\label{wxfiledetach}
6be663cf 139
247aba10
VZ
140\func{void}{Detach}{\void}
141
142Get back a file descriptor from wxFile object - the caller is responsible for closing the file if this
143descriptor is opened. \helpref{IsOpened()}{wxfileisopened} will return FALSE after call to Detach().
144
145\membersection{wxFile::fd}\label{wxfilefd}
6be663cf 146
247aba10
VZ
147\constfunc{int}{fd}{\void}
148
149Returns the file descriptor associated with the file.
150
a660d684
KB
151\membersection{wxFile::Eof}\label{wxfileeof}
152
153\constfunc{bool}{Eof}{\void}
154
c53a2bb3
VZ
155Returns TRUE if the end of the file has been reached.
156
157Note that the behaviour of the file pointer based class
158\helpref{wxFFile}{wxffile} is different as \helpref{wxFFile::Eof}{wxffileeof}
159will 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
161even before such attempt is made if the file pointer is at the last position
162in the file.
163
164Note also that this function doesn't work on unseekable file descriptors
165(examples include pipes, terminals and sockets under Unix) and an attempt to
f6bcfd97
BP
166use it will result in an error message in such case. So, to read the entire
167file into memory, you should write a loop which uses
168\helpref{Read}{wxfileread} repeatedly and tests its return condition instead
169of using Eof() as this will not work for special files under Unix.
a660d684
KB
170
171\membersection{wxFile::Exists}\label{wxfileexists}
172
173\func{static bool}{Exists}{\param{const char*}{ filename}}
174
e694c22c
VZ
175Returns TRUE if the given name specifies an existing regular file (not a
176directory or a link)
a660d684
KB
177
178\membersection{wxFile::Flush}\label{wxfileflush}
179
180\func{bool}{Flush}{\void}
181
247aba10
VZ
182Flushes the file descriptor.
183
184Note that wxFile::Flush is not implemented on some Windows compilers
185due 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).
a660d684
KB
187
188\membersection{wxFile::IsOpened}\label{wxfileisopened}
189
190\constfunc{bool}{IsOpened}{\void}
191
192Returns TRUE if the file has been opened.
193
194\membersection{wxFile::Length}\label{wxfilelength}
195
196\constfunc{off\_t}{Length}{\void}
197
198Returns 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
204Opens 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
216Reads 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
247aba10 226The number of bytes read, or the symbol {\bf wxInvalidOffset} (-1) if there was an error.
a660d684
KB
227
228\membersection{wxFile::Seek}\label{wxfileseek}
229
842d6c94 230\func{off\_t}{Seek}{\param{off\_t }{ofs}, \param{wxSeekMode }{mode = wxFromStart}}
a660d684
KB
231
232Seeks to the specified position.
233
234\wxheading{Parameters}
235
236\docparam{ofs}{Offset to seek to.}
237
842d6c94 238\docparam{mode}{One of {\bf wxFromStart}, {\bf wxFromEnd}, {\bf wxFromCurrent}.}
a660d684
KB
239
240\wxheading{Return value}
241
247aba10 242The actual offset position achieved, or wxInvalidOffset on failure.
a660d684
KB
243
244\membersection{wxFile::SeekEnd}\label{wxfileseekend}
245
246\func{off\_t}{SeekEnd}{\param{off\_t }{ofs = 0}}
247
248Moves 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
247aba10 256The actual offset position achieved, or wxInvalidOffset on failure.
a660d684
KB
257
258\membersection{wxFile::Tell}\label{wxfiletell}
259
260\constfunc{off\_t}{Tell}{\void}
261
247aba10 262Returns the current position or wxInvalidOffset if file is not opened or if another
f6bcfd97 263error occurred.
a660d684
KB
264
265\membersection{wxFile::Write}\label{wxfilewrite}
266
f6bcfd97 267\func{size\_t}{Write}{\param{const void*}{ buffer}, \param{off\_t}{ count}}
a660d684
KB
268
269Writes 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
f6bcfd97 279the number of bytes actually written
a660d684 280
247aba10 281\membersection{wxFile::Write}\label{wxfilewrites}
6be663cf 282
1facd32a 283\func{bool}{Write}{\param{const wxString\& }{s}, \param{wxMBConv&}{ conv = wxConvLibc}}
a660d684 284
6be663cf
JS
285Writes the contents of the string to the file, returns TRUE on success.
286
455df0f4 287The second argument is only meaningful in Unicode build of wxWindows when
1facd32a
VS
288{\it conv} is used to convert {\it s} to multibyte representation.
289
842d6c94
RR
290\section{\class{wxFFile}}\label{wxffile}
291
f6bcfd97 292wxFFile implements buffered file I/O. This is a very small class designed to
842d6c94
RR
293minimize the overhead of using it - in fact, there is hardly any overhead at
294all, but using it brings you automatic error checking and hides differences
f6bcfd97
BP
295between platforms and compilers. It wraps inside it a {\tt FILE *} handle used
296by standard C IO library (also known as {\tt stdio}).
842d6c94
RR
297
298\wxheading{Derived from}
299
300None.
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
319Default constructor.
320
321\func{}{wxFFile}{\param{const char*}{ filename}, \param{const char*}{ mode = "r"}}
322
323Opens a file with the given mode. As there is no way to return whether the
324operation was successful or not from the constructor you should test the
325return value of \helpref{IsOpened}{wxffileisopened} to check that it didn't
326fail.
327
328\func{}{wxFFile}{\param{FILE*}{ fp}}
329
330Opens a file with the given file pointer, which has already been opened.
331
332\wxheading{Parameters}
333
334\docparam{filename}{The filename.}
335
e379e258
VZ
336\docparam{mode}{The mode in which to open the file using standard C strings.
337Note that you should use {\tt "b"} flag if you use binary files under Windows
338or the results might be unexpected due to automatic newline conversion done
339for the text files.}
842d6c94
RR
340
341\docparam{fp}{An existing file descriptor, such as stderr.}
342
343\membersection{wxFFile::\destruct{wxFFile}}
344
345\func{}{\destruct{wxFFile}}{\void}
346
347Destructor will close the file.
348
349NB: 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
355Attaches an existing file pointer to the wxFFile object.
356
357The descriptor should be already opened and it will be closed by wxFFile
358object.
359
360\membersection{wxFFile::Close}\label{wxffileclose}
361
362\func{bool}{Close}{\void}
363
364Closes the file and returns TRUE on success.
365
366\membersection{wxFFile::Detach}\label{wxffiledetach}
367
368\func{void}{Detach}{\void}
369
370Get back a file pointer from wxFFile object - the caller is responsible for closing the file if this
371descriptor 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
377Returns the file pointer associated with the file.
378
379\membersection{wxFFile::Eof}\label{wxffileeof}
380
381\constfunc{bool}{Eof}{\void}
382
383Returns TRUE if the an attempt has been made to read {\it past}
384the end of the file.
385
386Note that the behaviour of the file descriptor based class
387\helpref{wxFile}{wxfile} is different as \helpref{wxFile::Eof}{wxfileeof}
388will return TRUE here as soon as the last byte of the file has been
389read.
390
391\membersection{wxFFile::Flush}\label{wxffileflush}
392
393\func{bool}{Flush}{\void}
394
395Flushes the file and returns TRUE on success.
396
397\membersection{wxFFile::IsOpened}\label{wxffileisopened}
398
399\constfunc{bool}{IsOpened}{\void}
400
401Returns TRUE if the file has been opened.
402
403\membersection{wxFFile::Length}\label{wxffilelength}
404
405\constfunc{size\_t}{Length}{\void}
406
407Returns 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
413Opens 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
425Reads 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
435The number of bytes read.
436
437\membersection{wxFFile::Seek}\label{wxffileseek}
438
439\func{bool}{Seek}{\param{long }{ofs}, \param{wxSeekMode }{mode = wxFromStart}}
440
441Seeks 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
453Moves the file pointer to the specified number of bytes before the end of the file
454and 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
464Returns the current position.
465
466\membersection{wxFFile::Write}\label{wxffilewrite}
467
605d715d 468\func{size\_t}{Write}{\param{const void*}{ buffer}, \param{size\_t}{ count}}
842d6c94
RR
469
470Writes 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
480Number of bytes written.
481
482\membersection{wxFFile::Write}\label{wxffilewrites}
483
1facd32a 484\func{bool}{Write}{\param{const wxString\& }{s}, \param{wxMBConv&}{ conv = wxConvLibc}}
842d6c94
RR
485
486Writes the contents of the string to the file, returns TRUE on success.
487
455df0f4 488The second argument is only meaningful in Unicode build of wxWindows when
1facd32a 489{\it conv} is used to convert {\it s} to multibyte representation.
567f21d2 490