1 \section{\class{wxFile
}}\label{wxfile
}
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.
11 \wxheading{Derived from
}
15 \wxheading{Include files
}
21 wx/file.h defines the following constants:
24 #define wxS_IRUSR
00400
25 #define wxS_IWUSR
00200
26 #define wxS_IXUSR
00100
28 #define wxS_IRGRP
00040
29 #define wxS_IWGRP
00020
30 #define wxS_IXGRP
00010
32 #define wxS_IROTH
00004
33 #define wxS_IWOTH
00002
34 #define wxS_IXOTH
00001
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)
40 These constants define the file access rights and are used with
41 \helpref{wxFile::Create
}{wxfilecreate
} and
\helpref{wxFile::Open
}{wxfileopen
}.
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.
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
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.
}
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
}:
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)
}
72 \latexignore{\rtfignore{\wxheading{Members
}}}
74 \membersection{wxFile::wxFile
}\label{wxfileconstr
}
76 \func{}{wxFile
}{\void}
80 \func{}{wxFile
}{\param{const char*
}{ filename
},
\param{wxFile::OpenMode
}{ mode = wxFile::read
}}
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
87 \func{}{wxFile
}{\param{int
}{ fd
}}
89 Associates the file with the given file descriptor, which has already been opened.
91 \wxheading{Parameters
}
93 \docparam{filename
}{The filename.
}
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}.
}
97 \docparam{fd
}{An existing file descriptor (see
\helpref{Attach()
}{wxfileattach
} for the list of predefined descriptors)
}
99 \membersection{wxFile::
\destruct{wxFile
}}
101 \func{}{\destruct{wxFile
}}{\void}
103 Destructor will close the file.
105 NB: it is not virtual so you should use wxFile polymorphically.
107 \membersection{wxFile::Access
}\label{wxfileaccess
}
109 \func{static bool
}{Access
}{\param{const char *
}{ name
},
\param{OpenMode
}{ mode
}}
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.
114 \membersection{wxFile::Attach
}\label{wxfileattach
}
116 \func{void
}{Attach
}{\param{int
}{ fd
}}
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}).
122 The descriptor should be already opened and it will be closed by wxFile
125 \membersection{wxFile::Close
}\label{wxfileclose
}
127 \func{void
}{Close
}{\void}
131 \membersection{wxFile::Create
}\label{wxfilecreate
}
133 \func{bool
}{Create
}{\param{const char*
}{ filename
},
\param{bool
}{ overwrite = FALSE
},
\param{int
}{access = wxS
\_DEFAULT}}
135 Creates a file for writing. If the file already exists, setting
{\bf overwrite
} to TRUE
136 will ensure it is overwritten.
138 \membersection{wxFile::Detach
}\label{wxfiledetach
}
140 \func{void
}{Detach
}{\void}
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().
145 \membersection{wxFile::fd
}\label{wxfilefd
}
147 \constfunc{int
}{fd
}{\void}
149 Returns the file descriptor associated with the file.
151 \membersection{wxFile::Eof
}\label{wxfileeof
}
153 \constfunc{bool
}{Eof
}{\void}
155 Returns TRUE if the end of the file has been reached.
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
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.
171 \membersection{wxFile::Exists
}\label{wxfileexists
}
173 \func{static bool
}{Exists
}{\param{const char*
}{ filename
}}
175 Returns TRUE if the given name specifies an existing regular file (not a
178 \membersection{wxFile::Flush
}\label{wxfileflush
}
180 \func{bool
}{Flush
}{\void}
182 Flushes the file descriptor.
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).
188 \membersection{wxFile::IsOpened
}\label{wxfileisopened
}
190 \constfunc{bool
}{IsOpened
}{\void}
192 Returns TRUE if the file has been opened.
194 \membersection{wxFile::Length
}\label{wxfilelength
}
196 \constfunc{off
\_t}{Length
}{\void}
198 Returns the length of the file.
200 \membersection{wxFile::Open
}\label{wxfileopen
}
202 \func{bool
}{Open
}{\param{const char*
}{ filename
},
\param{wxFile::OpenMode
}{ mode = wxFile::read
}}
204 Opens the file, returning TRUE if successful.
206 \wxheading{Parameters
}
208 \docparam{filename
}{The filename.
}
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}.
}
212 \membersection{wxFile::Read
}\label{wxfileread
}
214 \func{off
\_t}{Read
}{\param{void*
}{ buffer
},
\param{off
\_t}{ count
}}
216 Reads the specified number of bytes into a buffer, returning the actual number read.
218 \wxheading{Parameters
}
220 \docparam{buffer
}{A buffer to receive the data.
}
222 \docparam{count
}{The number of bytes to read.
}
224 \wxheading{Return value
}
226 The number of bytes read, or the symbol
{\bf wxInvalidOffset
} (-
1) if there was an error.
228 \membersection{wxFile::Seek
}\label{wxfileseek
}
230 \func{off
\_t}{Seek
}{\param{off
\_t }{ofs
},
\param{wxSeekMode
}{mode = wxFromStart
}}
232 Seeks to the specified position.
234 \wxheading{Parameters
}
236 \docparam{ofs
}{Offset to seek to.
}
238 \docparam{mode
}{One of
{\bf wxFromStart
},
{\bf wxFromEnd
},
{\bf wxFromCurrent
}.
}
240 \wxheading{Return value
}
242 The actual offset position achieved, or wxInvalidOffset on failure.
244 \membersection{wxFile::SeekEnd
}\label{wxfileseekend
}
246 \func{off
\_t}{SeekEnd
}{\param{off
\_t }{ofs =
0}}
248 Moves the file pointer to the specified number of bytes before the end of the file.
250 \wxheading{Parameters
}
252 \docparam{ofs
}{Number of bytes before the end of the file.
}
254 \wxheading{Return value
}
256 The actual offset position achieved, or wxInvalidOffset on failure.
258 \membersection{wxFile::Tell
}\label{wxfiletell
}
260 \constfunc{off
\_t}{Tell
}{\void}
262 Returns the current position or wxInvalidOffset if file is not opened or if another
265 \membersection{wxFile::Write
}\label{wxfilewrite
}
267 \func{size
\_t}{Write
}{\param{const void*
}{ buffer
},
\param{off
\_t}{ count
}}
269 Writes the specified number of bytes from a buffer.
271 \wxheading{Parameters
}
273 \docparam{buffer
}{A buffer containing the data.
}
275 \docparam{count
}{The number of bytes to write.
}
277 \wxheading{Return value
}
279 the number of bytes actually written
281 \membersection{wxFile::Write
}\label{wxfilewrites
}
283 \func{bool
}{Write
}{\param{const wxString\&
}{s
},
\param{wxMBConv&
}{ conv = wxConvLibc
}}
285 Writes the contents of the string to the file, returns TRUE on success.
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.
290 \section{\class{wxFFile
}}\label{wxffile
}
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
}).
298 \wxheading{Derived from
}
302 \wxheading{Include files
}
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)
}
313 \latexignore{\rtfignore{\wxheading{Members
}}}
315 \membersection{wxFFile::wxFFile
}\label{wxffileconstr
}
317 \func{}{wxFFile
}{\void}
321 \func{}{wxFFile
}{\param{const char*
}{ filename
},
\param{const char*
}{ mode = "r"
}}
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
328 \func{}{wxFFile
}{\param{FILE*
}{ fp
}}
330 Opens a file with the given file pointer, which has already been opened.
332 \wxheading{Parameters
}
334 \docparam{filename
}{The filename.
}
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
341 \docparam{fp
}{An existing file descriptor, such as stderr.
}
343 \membersection{wxFFile::
\destruct{wxFFile
}}
345 \func{}{\destruct{wxFFile
}}{\void}
347 Destructor will close the file.
349 NB: it is not virtual so you should
{\it not
} derive from wxFFile!
351 \membersection{wxFFile::Attach
}\label{wxffileattach
}
353 \func{void
}{Attach
}{\param{FILE*
}{ fp
}}
355 Attaches an existing file pointer to the wxFFile object.
357 The descriptor should be already opened and it will be closed by wxFFile
360 \membersection{wxFFile::Close
}\label{wxffileclose
}
362 \func{bool
}{Close
}{\void}
364 Closes the file and returns TRUE on success.
366 \membersection{wxFFile::Detach
}\label{wxffiledetach
}
368 \func{void
}{Detach
}{\void}
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().
373 \membersection{wxFFile::fp
}\label{wxffilefp
}
375 \constfunc{FILE *
}{fp
}{\void}
377 Returns the file pointer associated with the file.
379 \membersection{wxFFile::Eof
}\label{wxffileeof
}
381 \constfunc{bool
}{Eof
}{\void}
383 Returns TRUE if the an attempt has been made to read
{\it past
}
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
391 \membersection{wxFFile::Flush
}\label{wxffileflush
}
393 \func{bool
}{Flush
}{\void}
395 Flushes the file and returns TRUE on success.
397 \membersection{wxFFile::IsOpened
}\label{wxffileisopened
}
399 \constfunc{bool
}{IsOpened
}{\void}
401 Returns TRUE if the file has been opened.
403 \membersection{wxFFile::Length
}\label{wxffilelength
}
405 \constfunc{size
\_t}{Length
}{\void}
407 Returns the length of the file.
409 \membersection{wxFFile::Open
}\label{wxffileopen
}
411 \func{bool
}{Open
}{\param{const char*
}{ filename
},
\param{const char*
}{ mode = "r"
}}
413 Opens the file, returning TRUE if successful.
415 \wxheading{Parameters
}
417 \docparam{filename
}{The filename.
}
419 \docparam{mode
}{The mode in which to open the file.
}
421 \membersection{wxFFile::Read
}\label{wxffileread
}
423 \func{size
\_t}{Read
}{\param{void*
}{ buffer
},
\param{off
\_t}{ count
}}
425 Reads the specified number of bytes into a buffer, returning the actual number read.
427 \wxheading{Parameters
}
429 \docparam{buffer
}{A buffer to receive the data.
}
431 \docparam{count
}{The number of bytes to read.
}
433 \wxheading{Return value
}
435 The number of bytes read.
437 \membersection{wxFFile::Seek
}\label{wxffileseek
}
439 \func{bool
}{Seek
}{\param{long
}{ofs
},
\param{wxSeekMode
}{mode = wxFromStart
}}
441 Seeks to the specified position and returs TRUE on success.
443 \wxheading{Parameters
}
445 \docparam{ofs
}{Offset to seek to.
}
447 \docparam{mode
}{One of
{\bf wxFromStart
},
{\bf wxFromEnd
},
{\bf wxFromCurrent
}.
}
449 \membersection{wxFFile::SeekEnd
}\label{wxffileseekend
}
451 \func{bool
}{SeekEnd
}{\param{long
}{ofs =
0}}
453 Moves the file pointer to the specified number of bytes before the end of the file
454 and returns TRUE on success.
456 \wxheading{Parameters
}
458 \docparam{ofs
}{Number of bytes before the end of the file.
}
460 \membersection{wxFFile::Tell
}\label{wxffiletell
}
462 \constfunc{size
\_t}{Tell
}{\void}
464 Returns the current position.
466 \membersection{wxFFile::Write
}\label{wxffilewrite
}
468 \func{size
\_t}{Write
}{\param{const void*
}{ buffer
},
\param{size
\_t}{ count
}}
470 Writes the specified number of bytes from a buffer.
472 \wxheading{Parameters
}
474 \docparam{buffer
}{A buffer containing the data.
}
476 \docparam{count
}{The number of bytes to write.
}
478 \wxheading{Return value
}
480 Number of bytes written.
482 \membersection{wxFFile::Write
}\label{wxffilewrites
}
484 \func{bool
}{Write
}{\param{const wxString\&
}{s
},
\param{wxMBConv&
}{ conv = wxConvLibc
}}
486 Writes the contents of the string to the file, returns TRUE on success.
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.