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.
9 \wxheading{Derived from
}
13 \wxheading{Include files
}
19 wx/file.h defines the following constants:
23 #define wxS_IRUSR
00400
24 #define wxS_IWUSR
00200
25 #define wxS_IXUSR
00100
27 #define wxS_IRGRP
00040
28 #define wxS_IWGRP
00020
29 #define wxS_IXGRP
00010
31 #define wxS_IROTH
00004
32 #define wxS_IWOTH
00002
33 #define wxS_IXOTH
00001
35 // default mode for the new files: corresponds to umask
022
36 #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's 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()
}
58 Other constants defined elsewhere but used by wxFile functions are wxInvalidOffset which represents an
59 invalid value of type
{\it off
\_t} and is returned by functions returning
{\it off
\_t} on error and the seek
60 mode constants used with
\helpref{Seek()
}{wxfileseek
}:
63 \begin{twocollist
}\itemsep=
0pt
%
64 \twocolitem{{\bf wxFromStart
}}{Count offset from the start of the file
}
65 \twocolitem{{\bf wxFromCurrent
}}{Count offset from the current position of the file pointer
}
66 \twocolitem{{\bf wxFromEnd
}}{Count offset from the end of the file (backwards)
}
69 \latexignore{\rtfignore{\wxheading{Members
}}}
71 \membersection{wxFile::wxFile
}\label{wxfileconstr
}
73 \func{}{wxFile
}{\void}
77 \func{}{wxFile
}{\param{const char*
}{ filename
},
\param{wxFile::OpenMode
}{ mode = wxFile::read
}}
79 Opens a file with the given mode. As there is no way to return whether the
80 operation was successful or not from the constructor you should test the
81 return value of
\helpref{IsOpened
}{wxfileisopened
} to check that it didn't
84 \func{}{wxFile
}{\param{int
}{ fd
}}
86 Associates the file with the given file descriptor, which has already been opened.
88 \wxheading{Parameters
}
90 \docparam{filename
}{The filename.
}
92 \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 \docparam{fd
}{An existing file descriptor (see
\helpref{Attach()
}{wxfileattach
} for the list of predefined descriptors)
}
96 \membersection{wxFile::
\destruct{wxFile
}}
98 \func{}{\destruct{wxFile
}}{\void}
100 Destructor will close the file.
102 NB: it is not virtual so you should use wxFile polymorphically.
104 \membersection{wxFile::Access
}\label{wxfileaccess
}
106 \func{static bool
}{Access
}{\param{const char *
}{ name
},
\param{OpenMode
}{ mode
}}
108 This function verifies if we may access the given file in specified mode. Only
109 values of wxFile::read or wxFile::write really make sense here.
111 \membersection{wxFile::Attach
}\label{wxfileattach
}
113 \func{void
}{Attach
}{\param{int
}{ fd
}}
115 Attaches an existing file descriptor to the wxFile object. Example of predefined
116 file descriptors are
0,
1 and
2 which correspond to stdin, stdout and stderr (and
117 have symbolic names of wxFile::fd
\_stdin, wxFile::fd
\_stdout and wxFile::fd
\_stderr).
119 The descriptor should be already opened and it will be closed by wxFile
122 \membersection{wxFile::Close
}\label{wxfileclose
}
124 \func{void
}{Close
}{\void}
128 \membersection{wxFile::Create
}\label{wxfilecreate
}
130 \func{bool
}{Create
}{\param{const char*
}{ filename
},
\param{bool
}{ overwrite = FALSE
},
\param{int
}{access = wxS
\_DEFAULT}}
132 Creates a file for writing. If the file already exists, setting
{\bf overwrite
} to TRUE
133 will ensure it is overwritten.
135 \membersection{wxFile::Detach
}\label{wxfiledetach
}
137 \func{void
}{Detach
}{\void}
139 Get back a file descriptor from wxFile object - the caller is responsible for closing the file if this
140 descriptor is opened.
\helpref{IsOpened()
}{wxfileisopened
} will return FALSE after call to Detach().
142 \membersection{wxFile::fd
}\label{wxfilefd
}
144 \constfunc{int
}{fd
}{\void}
146 Returns the file descriptor associated with the file.
148 \membersection{wxFile::Eof
}\label{wxfileeof
}
150 \constfunc{bool
}{Eof
}{\void}
152 Returns TRUE if the end of the file has been reached.
154 Note that the behaviour of the file pointer based class
155 \helpref{wxFFile
}{wxffile
} is different as
\helpref{wxFFile::Eof
}{wxffileeof
}
156 will return TRUE here only if an attempt has been made to read
157 {\it past
} the last byte of the file, while wxFile::Eof() will return TRUE
158 even before such attempt is made if the file pointer is at the last position
161 Note also that this function doesn't work on unseekable file descriptors
162 (examples include pipes, terminals and sockets under Unix) and an attempt to
163 use it will result in an error message in such case.
165 \membersection{wxFile::Exists
}\label{wxfileexists
}
167 \func{static bool
}{Exists
}{\param{const char*
}{ filename
}}
169 Returns TRUE if the given name specifies an existing regular file (not a
172 \membersection{wxFile::Flush
}\label{wxfileflush
}
174 \func{bool
}{Flush
}{\void}
176 Flushes the file descriptor.
178 Note that wxFile::Flush is not implemented on some Windows compilers
179 due to a missing fsync function, which reduces the usefulness of this function
180 (it can still be called but it will do nothing on unsupported compilers).
182 \membersection{wxFile::IsOpened
}\label{wxfileisopened
}
184 \constfunc{bool
}{IsOpened
}{\void}
186 Returns TRUE if the file has been opened.
188 \membersection{wxFile::Length
}\label{wxfilelength
}
190 \constfunc{off
\_t}{Length
}{\void}
192 Returns the length of the file.
194 \membersection{wxFile::Open
}\label{wxfileopen
}
196 \func{bool
}{Open
}{\param{const char*
}{ filename
},
\param{wxFile::OpenMode
}{ mode = wxFile::read
}}
198 Opens the file, returning TRUE if successful.
200 \wxheading{Parameters
}
202 \docparam{filename
}{The filename.
}
204 \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}.
}
206 \membersection{wxFile::Read
}\label{wxfileread
}
208 \func{off
\_t}{Read
}{\param{void*
}{ buffer
},
\param{off
\_t}{ count
}}
210 Reads the specified number of bytes into a buffer, returning the actual number read.
212 \wxheading{Parameters
}
214 \docparam{buffer
}{A buffer to receive the data.
}
216 \docparam{count
}{The number of bytes to read.
}
218 \wxheading{Return value
}
220 The number of bytes read, or the symbol
{\bf wxInvalidOffset
} (-
1) if there was an error.
222 \membersection{wxFile::Seek
}\label{wxfileseek
}
224 \func{off
\_t}{Seek
}{\param{off
\_t }{ofs
},
\param{wxSeekMode
}{mode = wxFromStart
}}
226 Seeks to the specified position.
228 \wxheading{Parameters
}
230 \docparam{ofs
}{Offset to seek to.
}
232 \docparam{mode
}{One of
{\bf wxFromStart
},
{\bf wxFromEnd
},
{\bf wxFromCurrent
}.
}
234 \wxheading{Return value
}
236 The actual offset position achieved, or wxInvalidOffset on failure.
238 \membersection{wxFile::SeekEnd
}\label{wxfileseekend
}
240 \func{off
\_t}{SeekEnd
}{\param{off
\_t }{ofs =
0}}
242 Moves the file pointer to the specified number of bytes before the end of the file.
244 \wxheading{Parameters
}
246 \docparam{ofs
}{Number of bytes before the end of the file.
}
248 \wxheading{Return value
}
250 The actual offset position achieved, or wxInvalidOffset on failure.
252 \membersection{wxFile::Tell
}\label{wxfiletell
}
254 \constfunc{off
\_t}{Tell
}{\void}
256 Returns the current position or wxInvalidOffset if file is not opened or if another
259 \membersection{wxFile::Write
}\label{wxfilewrite
}
261 \func{bool
}{Write
}{\param{const void*
}{ buffer
},
\param{off
\_t}{ count
}}
263 Writes the specified number of bytes from a buffer.
265 \wxheading{Parameters
}
267 \docparam{buffer
}{A buffer containing the data.
}
269 \docparam{count
}{The number of bytes to write.
}
271 \wxheading{Return value
}
273 TRUE if the operation was successful.
275 \membersection{wxFile::Write
}\label{wxfilewrites
}
277 \func{bool
}{Write
}{\param{const wxString\&
}{s
}}
279 Writes the contents of the string to the file, returns TRUE on success.
281 \section{\class{wxFFile
}}\label{wxffile
}
283 A wxFFile performs raw file I/O. This is a very small class designed to
284 minimize the overhead of using it - in fact, there is hardly any overhead at
285 all, but using it brings you automatic error checking and hides differences
286 between platforms and compilers.
288 \wxheading{Derived from
}
292 \wxheading{Include files
}
297 \begin{twocollist
}\itemsep=
0pt
%
298 \twocolitem{{\bf wxFromStart
}}{Count offset from the start of the file
}
299 \twocolitem{{\bf wxFromCurrent
}}{Count offset from the current position of the file pointer
}
300 \twocolitem{{\bf wxFromEnd
}}{Count offset from the end of the file (backwards)
}
303 \latexignore{\rtfignore{\wxheading{Members
}}}
305 \membersection{wxFFile::wxFFile
}\label{wxffileconstr
}
307 \func{}{wxFFile
}{\void}
311 \func{}{wxFFile
}{\param{const char*
}{ filename
},
\param{const char*
}{ mode = "r"
}}
313 Opens a file with the given mode. As there is no way to return whether the
314 operation was successful or not from the constructor you should test the
315 return value of
\helpref{IsOpened
}{wxffileisopened
} to check that it didn't
318 \func{}{wxFFile
}{\param{FILE*
}{ fp
}}
320 Opens a file with the given file pointer, which has already been opened.
322 \wxheading{Parameters
}
324 \docparam{filename
}{The filename.
}
326 \docparam{mode
}{The mode in which to open the file using standard C strings.
}
328 \docparam{fp
}{An existing file descriptor, such as stderr.
}
330 \membersection{wxFFile::
\destruct{wxFFile
}}
332 \func{}{\destruct{wxFFile
}}{\void}
334 Destructor will close the file.
336 NB: it is not virtual so you should
{\it not
} derive from wxFFile!
338 \membersection{wxFFile::Attach
}\label{wxffileattach
}
340 \func{void
}{Attach
}{\param{FILE*
}{ fp
}}
342 Attaches an existing file pointer to the wxFFile object.
344 The descriptor should be already opened and it will be closed by wxFFile
347 \membersection{wxFFile::Close
}\label{wxffileclose
}
349 \func{bool
}{Close
}{\void}
351 Closes the file and returns TRUE on success.
353 \membersection{wxFFile::Detach
}\label{wxffiledetach
}
355 \func{void
}{Detach
}{\void}
357 Get back a file pointer from wxFFile object - the caller is responsible for closing the file if this
358 descriptor is opened.
\helpref{IsOpened()
}{wxffileisopened
} will return FALSE after call to Detach().
360 \membersection{wxFFile::fp
}\label{wxffilefp
}
362 \constfunc{FILE *
}{fp
}{\void}
364 Returns the file pointer associated with the file.
366 \membersection{wxFFile::Eof
}\label{wxffileeof
}
368 \constfunc{bool
}{Eof
}{\void}
370 Returns TRUE if the an attempt has been made to read
{\it past
}
373 Note that the behaviour of the file descriptor based class
374 \helpref{wxFile
}{wxfile
} is different as
\helpref{wxFile::Eof
}{wxfileeof
}
375 will return TRUE here as soon as the last byte of the file has been
378 \membersection{wxFFile::Flush
}\label{wxffileflush
}
380 \func{bool
}{Flush
}{\void}
382 Flushes the file and returns TRUE on success.
384 \membersection{wxFFile::IsOpened
}\label{wxffileisopened
}
386 \constfunc{bool
}{IsOpened
}{\void}
388 Returns TRUE if the file has been opened.
390 \membersection{wxFFile::Length
}\label{wxffilelength
}
392 \constfunc{size
\_t}{Length
}{\void}
394 Returns the length of the file.
396 \membersection{wxFFile::Open
}\label{wxffileopen
}
398 \func{bool
}{Open
}{\param{const char*
}{ filename
},
\param{const char*
}{ mode = "r"
}}
400 Opens the file, returning TRUE if successful.
402 \wxheading{Parameters
}
404 \docparam{filename
}{The filename.
}
406 \docparam{mode
}{The mode in which to open the file.
}
408 \membersection{wxFFile::Read
}\label{wxffileread
}
410 \func{size
\_t}{Read
}{\param{void*
}{ buffer
},
\param{off
\_t}{ count
}}
412 Reads the specified number of bytes into a buffer, returning the actual number read.
414 \wxheading{Parameters
}
416 \docparam{buffer
}{A buffer to receive the data.
}
418 \docparam{count
}{The number of bytes to read.
}
420 \wxheading{Return value
}
422 The number of bytes read.
424 \membersection{wxFFile::Seek
}\label{wxffileseek
}
426 \func{bool
}{Seek
}{\param{long
}{ofs
},
\param{wxSeekMode
}{mode = wxFromStart
}}
428 Seeks to the specified position and returs TRUE on success.
430 \wxheading{Parameters
}
432 \docparam{ofs
}{Offset to seek to.
}
434 \docparam{mode
}{One of
{\bf wxFromStart
},
{\bf wxFromEnd
},
{\bf wxFromCurrent
}.
}
436 \membersection{wxFFile::SeekEnd
}\label{wxffileseekend
}
438 \func{bool
}{SeekEnd
}{\param{long
}{ofs =
0}}
440 Moves the file pointer to the specified number of bytes before the end of the file
441 and returns TRUE on success.
443 \wxheading{Parameters
}
445 \docparam{ofs
}{Number of bytes before the end of the file.
}
447 \membersection{wxFFile::Tell
}\label{wxffiletell
}
449 \constfunc{size
\_t}{Tell
}{\void}
451 Returns the current position.
453 \membersection{wxFFile::Write
}\label{wxffilewrite
}
455 \func{size_t
}{Write
}{\param{const void*
}{ buffer
},
\param{size
\_t}{ count
}}
457 Writes the specified number of bytes from a buffer.
459 \wxheading{Parameters
}
461 \docparam{buffer
}{A buffer containing the data.
}
463 \docparam{count
}{The number of bytes to write.
}
465 \wxheading{Return value
}
467 Number of bytes written.
469 \membersection{wxFFile::Write
}\label{wxffilewrites
}
471 \func{bool
}{Write
}{\param{const wxString\&
}{s
}}
473 Writes the contents of the string to the file, returns TRUE on success.