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