wxFFile implements buffered file I/O. This is a very small class designed to
minimize the overhead of using it - in fact, there is hardly any overhead at
all, but using it brings you automatic error checking and hides differences
between platforms and compilers. It wraps inside it a FILE * handle used
by standard C IO library (also known as stdio).
Count offset from the start of the file
Count offset from the current position of the file pointer
Count offset from the end of the file (backwards)
wxFFile(const char* filename, const char* mode = "r")
Opens a file with the given mode. As there is no way to return whether the
operation was successful or not from the constructor you should test the
return value of IsOpened
to check that it didn't
Opens a file with the given file pointer, which has already been opened.
The mode in which to open the file using standard C strings.
Note that you should use "b" flag if you use binary files under Windows
or the results might be unexpected due to automatic newline conversion done
for the text files.
An existing file descriptor, such as stderr.
Destructor will close the file.
NB: it is not virtual so you should not derive from wxFFile!
void Attach(FILE* fp)
Attaches an existing file pointer to the wxFFile object.
The descriptor should be already opened and it will be closed by wxFFile
Closes the file and returns true on success.
Get back a file pointer from wxFFile object -- the caller is responsible for closing the file if this
descriptor is opened. IsOpened()
will return false
after call to Detach().
FILE * fp() const
Returns the file pointer associated with the file.
bool Eof() const
Returns true if the an attempt has been made to read past
the end of the file.
Note that the behaviour of the file descriptor based class
is different as wxFile::Eof
will return true
here as soon as the last byte of the file has been
Also note that this method may only be called for opened files and may crash if
the file is not opened.
Returns true if an error has occurred on this file, similar to the standard
Please note that this method may only be called for opened files and may crash
if the file is not opened.
Flushes the file and returns true on success.
wxFileKind GetKind() const
Returns the type of the file. Possible return values are:
wxFILE_KIND_DISK, // a file supporting seeking to arbitrary offsets
wxFILE_KIND_TERMINAL, // a tty
wxFILE_KIND_PIPE // a pipe
bool IsOpened() const
Returns true if the file is opened. Most of the methods of this class may only
be used for an opened file.
wxFileOffset Length() const
Returns the length of the file.
bool Open(const char* filename, const char* mode = "r")
Opens the file, returning true if successful.
The mode in which to open the file.
size_t Read(void* buffer, size_t count)
Reads the specified number of bytes into a buffer, returning the actual number read.
A buffer to receive the data.
The number of bytes to read.
The number of bytes read.
Reads the entire contents of the file into a string.
String to read data into.
Conversion object to use in Unicode build; by default supposes
that file contents is encoded in UTF-8.
true if file was read successfully, false otherwise.
bool Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart)
Seeks to the specified position and returns true on success.
Offset to seek to.
One of wxFromStart, wxFromEnd, wxFromCurrent.
bool SeekEnd(wxFileOffset ofs = 0)
Moves the file pointer to the specified number of bytes before the end of the file
and returns true on success.
Number of bytes before the end of the file.
wxFileOffset Tell() const
Returns the current position.
size_t Write(const void* buffer, size_t count)
Writes the specified number of bytes from a buffer.
A buffer containing the data.
The number of bytes to write.
Number of bytes written.
Writes the contents of the string to the file, returns true on success.
The second argument is only meaningful in Unicode build of wxWidgets when
conv is used to convert s to multibyte representation.