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
59 Other constants defined elsewhere but used by wxFile functions are wxInvalidOffset which represents an
60 invalid value of type
{\it off
\_t} and is returned by functions returning
{\it off
\_t} on error and the seek
61 mode constants used with
\helpref{Seek()
}{wxfileseek
}:
64 \begin{twocollist
}\itemsep=
0pt
%
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)
}
70 \latexignore{\rtfignore{\wxheading{Members
}}}
72 \membersection{wxFile::wxFile
}\label{wxfileconstr
}
74 \func{}{wxFile
}{\void}
78 \func{}{wxFile
}{\param{const char*
}{ filename
},
\param{wxFile::OpenMode
}{ mode = wxFile::read
}}
80 Opens a file with the given mode. As there is no way to return whether the
81 operation was successful or not from the constructor you should test the
82 return value of
\helpref{IsOpened
}{wxfileisopened
} to check that it didn't
85 \func{}{wxFile
}{\param{int
}{ fd
}}
87 Associates the file with the given file descriptor, which has already been opened.
89 \wxheading{Parameters
}
91 \docparam{filename
}{The filename.
}
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}.
}
95 \docparam{fd
}{An existing file descriptor (see
\helpref{Attach()
}{wxfileattach
} for the list of predefined descriptors)
}
97 \membersection{wxFile::
\destruct{wxFile
}}
99 \func{}{\destruct{wxFile
}}{\void}
101 Destructor will close the file.
103 NB: it is not virtual so you should use wxFile polymorphically.
105 \membersection{wxFile::Access
}\label{wxfileaccess
}
107 \func{static bool
}{Access
}{\param{const char *
}{ name
},
\param{OpenMode
}{ mode
}}
109 This function verifies if we may access the given file in specified mode. Only
110 values of wxFile::read or wxFile::write really make sense here.
112 \membersection{wxFile::Attach
}\label{wxfileattach
}
114 \func{void
}{Attach
}{\param{int
}{ fd
}}
116 Attaches an existing file descriptor to the wxFile object. Example of predefined
117 file descriptors are
0,
1 and
2 which correspond to stdin, stdout and stderr (and
118 have symbolic names of
{\bf wxFile::fd
\_stdin},
{\bf wxFile::fd
\_stdout} and
{\bf wxFile::fd
\_stderr}).
120 The descriptor should be already opened and it will be closed by wxFile
123 \membersection{wxFile::Close
}\label{wxfileclose
}
125 \func{void
}{Close
}{\void}
129 \membersection{wxFile::Create
}\label{wxfilecreate
}
131 \func{bool
}{Create
}{\param{const char*
}{ filename
},
\param{bool
}{ overwrite = FALSE
},
\param{int
}{access = wxS
\_DEFAULT}}
133 Creates a file for writing. If the file already exists, setting
{\bf overwrite
} to TRUE
134 will ensure it is overwritten.
136 \membersection{wxFile::Detach
}\label{wxfiledetach
}
138 \func{void
}{Detach
}{\void}
140 Get back a file descriptor from wxFile object - the caller is responsible for closing the file if this
141 descriptor is opened.
\helpref{IsOpened()
}{wxfileisopened
} will return FALSE after call to Detach().
143 \membersection{wxFile::fd
}\label{wxfilefd
}
145 \constfunc{int
}{fd
}{\void}
147 Returns the file descriptor associated with the file.
149 \membersection{wxFile::Eof
}\label{wxfileeof
}
151 \constfunc{bool
}{Eof
}{\void}
153 Returns TRUE if the end of the file has been reached.
155 Note that the behaviour of the file pointer based class
156 \helpref{wxFFile
}{wxffile
} is different as
\helpref{wxFFile::Eof
}{wxffileeof
}
157 will 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
159 even before such attempt is made if the file pointer is at the last position
162 Note also that this function doesn't work on unseekable file descriptors
163 (examples include pipes, terminals and sockets under Unix) and an attempt to
164 use it will result in an error message in such case. So, to read the entire
165 file into memory, you should write a loop which uses
166 \helpref{Read
}{wxfileread
} repeatedly and tests its return condition instead
167 of using Eof() as this will not work for special files under Unix.
169 \membersection{wxFile::Exists
}\label{wxfileexists
}
171 \func{static bool
}{Exists
}{\param{const char*
}{ filename
}}
173 Returns TRUE if the given name specifies an existing regular file (not a
176 \membersection{wxFile::Flush
}\label{wxfileflush
}
178 \func{bool
}{Flush
}{\void}
180 Flushes the file descriptor.
182 Note that wxFile::Flush is not implemented on some Windows compilers
183 due 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).
186 \membersection{wxFile::IsOpened
}\label{wxfileisopened
}
188 \constfunc{bool
}{IsOpened
}{\void}
190 Returns TRUE if the file has been opened.
192 \membersection{wxFile::Length
}\label{wxfilelength
}
194 \constfunc{off
\_t}{Length
}{\void}
196 Returns the length of the file.
198 \membersection{wxFile::Open
}\label{wxfileopen
}
200 \func{bool
}{Open
}{\param{const char*
}{ filename
},
\param{wxFile::OpenMode
}{ mode = wxFile::read
}}
202 Opens the file, returning TRUE if successful.
204 \wxheading{Parameters
}
206 \docparam{filename
}{The filename.
}
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}.
}
210 \membersection{wxFile::Read
}\label{wxfileread
}
212 \func{off
\_t}{Read
}{\param{void*
}{ buffer
},
\param{off
\_t}{ count
}}
214 Reads the specified number of bytes into a buffer, returning the actual number read.
216 \wxheading{Parameters
}
218 \docparam{buffer
}{A buffer to receive the data.
}
220 \docparam{count
}{The number of bytes to read.
}
222 \wxheading{Return value
}
224 The number of bytes read, or the symbol
{\bf wxInvalidOffset
} (-
1) if there was an error.
226 \membersection{wxFile::Seek
}\label{wxfileseek
}
228 \func{off
\_t}{Seek
}{\param{off
\_t }{ofs
},
\param{wxSeekMode
}{mode = wxFromStart
}}
230 Seeks to the specified position.
232 \wxheading{Parameters
}
234 \docparam{ofs
}{Offset to seek to.
}
236 \docparam{mode
}{One of
{\bf wxFromStart
},
{\bf wxFromEnd
},
{\bf wxFromCurrent
}.
}
238 \wxheading{Return value
}
240 The actual offset position achieved, or wxInvalidOffset on failure.
242 \membersection{wxFile::SeekEnd
}\label{wxfileseekend
}
244 \func{off
\_t}{SeekEnd
}{\param{off
\_t }{ofs =
0}}
246 Moves the file pointer to the specified number of bytes before the end of the file.
248 \wxheading{Parameters
}
250 \docparam{ofs
}{Number of bytes before the end of the file.
}
252 \wxheading{Return value
}
254 The actual offset position achieved, or wxInvalidOffset on failure.
256 \membersection{wxFile::Tell
}\label{wxfiletell
}
258 \constfunc{off
\_t}{Tell
}{\void}
260 Returns the current position or wxInvalidOffset if file is not opened or if another
263 \membersection{wxFile::Write
}\label{wxfilewrite
}
265 \func{size
\_t}{Write
}{\param{const void*
}{ buffer
},
\param{off
\_t}{ count
}}
267 Writes the specified number of bytes from a buffer.
269 \wxheading{Parameters
}
271 \docparam{buffer
}{A buffer containing the data.
}
273 \docparam{count
}{The number of bytes to write.
}
275 \wxheading{Return value
}
277 the number of bytes actually written
279 \membersection{wxFile::Write
}\label{wxfilewrites
}
281 \func{bool
}{Write
}{\param{const wxString\&
}{s
}}
283 Writes the contents of the string to the file, returns TRUE on success.
285 \section{\class{wxFFile
}}\label{wxffile
}
287 wxFFile implements buffered file I/O. This is a very small class designed to
288 minimize the overhead of using it - in fact, there is hardly any overhead at
289 all, but using it brings you automatic error checking and hides differences
290 between platforms and compilers. It wraps inside it a
{\tt FILE *
} handle used
291 by standard C IO library (also known as
{\tt stdio
}).
293 \wxheading{Derived from
}
297 \wxheading{Include files
}
302 \begin{twocollist
}\itemsep=
0pt
%
303 \twocolitem{{\bf wxFromStart
}}{Count offset from the start of the file
}
304 \twocolitem{{\bf wxFromCurrent
}}{Count offset from the current position of the file pointer
}
305 \twocolitem{{\bf wxFromEnd
}}{Count offset from the end of the file (backwards)
}
308 \latexignore{\rtfignore{\wxheading{Members
}}}
310 \membersection{wxFFile::wxFFile
}\label{wxffileconstr
}
312 \func{}{wxFFile
}{\void}
316 \func{}{wxFFile
}{\param{const char*
}{ filename
},
\param{const char*
}{ mode = "r"
}}
318 Opens a file with the given mode. As there is no way to return whether the
319 operation was successful or not from the constructor you should test the
320 return value of
\helpref{IsOpened
}{wxffileisopened
} to check that it didn't
323 \func{}{wxFFile
}{\param{FILE*
}{ fp
}}
325 Opens a file with the given file pointer, which has already been opened.
327 \wxheading{Parameters
}
329 \docparam{filename
}{The filename.
}
331 \docparam{mode
}{The mode in which to open the file using standard C strings.
332 Note that you should use
{\tt "b"
} flag if you use binary files under Windows
333 or the results might be unexpected due to automatic newline conversion done
336 \docparam{fp
}{An existing file descriptor, such as stderr.
}
338 \membersection{wxFFile::
\destruct{wxFFile
}}
340 \func{}{\destruct{wxFFile
}}{\void}
342 Destructor will close the file.
344 NB: it is not virtual so you should
{\it not
} derive from wxFFile!
346 \membersection{wxFFile::Attach
}\label{wxffileattach
}
348 \func{void
}{Attach
}{\param{FILE*
}{ fp
}}
350 Attaches an existing file pointer to the wxFFile object.
352 The descriptor should be already opened and it will be closed by wxFFile
355 \membersection{wxFFile::Close
}\label{wxffileclose
}
357 \func{bool
}{Close
}{\void}
359 Closes the file and returns TRUE on success.
361 \membersection{wxFFile::Detach
}\label{wxffiledetach
}
363 \func{void
}{Detach
}{\void}
365 Get back a file pointer from wxFFile object - the caller is responsible for closing the file if this
366 descriptor is opened.
\helpref{IsOpened()
}{wxffileisopened
} will return FALSE after call to Detach().
368 \membersection{wxFFile::fp
}\label{wxffilefp
}
370 \constfunc{FILE *
}{fp
}{\void}
372 Returns the file pointer associated with the file.
374 \membersection{wxFFile::Eof
}\label{wxffileeof
}
376 \constfunc{bool
}{Eof
}{\void}
378 Returns TRUE if the an attempt has been made to read
{\it past
}
381 Note that the behaviour of the file descriptor based class
382 \helpref{wxFile
}{wxfile
} is different as
\helpref{wxFile::Eof
}{wxfileeof
}
383 will return TRUE here as soon as the last byte of the file has been
386 \membersection{wxFFile::Flush
}\label{wxffileflush
}
388 \func{bool
}{Flush
}{\void}
390 Flushes the file and returns TRUE on success.
392 \membersection{wxFFile::IsOpened
}\label{wxffileisopened
}
394 \constfunc{bool
}{IsOpened
}{\void}
396 Returns TRUE if the file has been opened.
398 \membersection{wxFFile::Length
}\label{wxffilelength
}
400 \constfunc{size
\_t}{Length
}{\void}
402 Returns the length of the file.
404 \membersection{wxFFile::Open
}\label{wxffileopen
}
406 \func{bool
}{Open
}{\param{const char*
}{ filename
},
\param{const char*
}{ mode = "r"
}}
408 Opens the file, returning TRUE if successful.
410 \wxheading{Parameters
}
412 \docparam{filename
}{The filename.
}
414 \docparam{mode
}{The mode in which to open the file.
}
416 \membersection{wxFFile::Read
}\label{wxffileread
}
418 \func{size
\_t}{Read
}{\param{void*
}{ buffer
},
\param{off
\_t}{ count
}}
420 Reads the specified number of bytes into a buffer, returning the actual number read.
422 \wxheading{Parameters
}
424 \docparam{buffer
}{A buffer to receive the data.
}
426 \docparam{count
}{The number of bytes to read.
}
428 \wxheading{Return value
}
430 The number of bytes read.
432 \membersection{wxFFile::Seek
}\label{wxffileseek
}
434 \func{bool
}{Seek
}{\param{long
}{ofs
},
\param{wxSeekMode
}{mode = wxFromStart
}}
436 Seeks to the specified position and returs TRUE on success.
438 \wxheading{Parameters
}
440 \docparam{ofs
}{Offset to seek to.
}
442 \docparam{mode
}{One of
{\bf wxFromStart
},
{\bf wxFromEnd
},
{\bf wxFromCurrent
}.
}
444 \membersection{wxFFile::SeekEnd
}\label{wxffileseekend
}
446 \func{bool
}{SeekEnd
}{\param{long
}{ofs =
0}}
448 Moves the file pointer to the specified number of bytes before the end of the file
449 and returns TRUE on success.
451 \wxheading{Parameters
}
453 \docparam{ofs
}{Number of bytes before the end of the file.
}
455 \membersection{wxFFile::Tell
}\label{wxffiletell
}
457 \constfunc{size
\_t}{Tell
}{\void}
459 Returns the current position.
461 \membersection{wxFFile::Write
}\label{wxffilewrite
}
463 \func{size
\_t}{Write
}{\param{const void*
}{ buffer
},
\param{size
\_t}{ count
}}
465 Writes the specified number of bytes from a buffer.
467 \wxheading{Parameters
}
469 \docparam{buffer
}{A buffer containing the data.
}
471 \docparam{count
}{The number of bytes to write.
}
473 \wxheading{Return value
}
475 Number of bytes written.
477 \membersection{wxFFile::Write
}\label{wxffilewrites
}
479 \func{bool
}{Write
}{\param{const wxString\&
}{s
}}
481 Writes the contents of the string to the file, returns TRUE on success.