#include <wx/file.h>
A wxFile performs raw 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. wxFile also automatically closes the file in its destructor so you won't forget to do so. wxFile is a wrapper around file
descriptor. - see also wxFFile for a wrapper around FILE
structure.
wxFileOffset is used by the wxFile functions which require offsets as parameter or return them. If the platform supports it, wxFileOffset is a typedef for a native 64 bit integer, otherwise a 32 bit integer is used for wxFileOffset.
Public Types | |
enum | OpenMode { read , write , read_write , write_append , write_excl } |
The OpenMode enumeration defines the different modes for opening a file with wxFile. More... | |
enum | { fd_invalid = -1 , fd_stdin , fd_stdout , fd_stderr } |
Standard file descriptors. More... | |
Public Member Functions | |
wxFile () | |
Default constructor. More... | |
wxFile (const wxString &filename, wxFile::OpenMode mode=wxFile::read) | |
Opens a file with a filename. More... | |
wxFile (int fd) | |
Associates the file with the given file descriptor, which has already been opened. More... | |
~wxFile () | |
Destructor will close the file. More... | |
int | GetLastError () const |
Returns the error code for the last unsuccessful operation. More... | |
void | ClearLastError () |
Resets the error code. More... | |
void | Attach (int fd) |
Attaches an existing file descriptor to the wxFile object. More... | |
bool | Close () |
Closes the file. More... | |
bool | Create (const wxString &filename, bool overwrite=false, int access=wxS_DEFAULT) |
Creates a file for writing. More... | |
int | Detach () |
Get back a file descriptor from wxFile object - the caller is responsible for closing the file if this descriptor is opened. More... | |
bool | Eof () const |
Returns true if the end of the file has been reached. More... | |
bool | Flush () |
Flushes the file descriptor. More... | |
wxFileKind | GetKind () const |
Returns the type of the file. More... | |
bool | IsOpened () const |
Returns true if the file has been opened. More... | |
wxFileOffset | Length () const |
Returns the length of the file. More... | |
bool | Open (const wxString &filename, wxFile::OpenMode mode=wxFile::read, int access=wxS_DEFAULT) |
Opens the file, returning true if successful. More... | |
ssize_t | Read (void *buffer, size_t count) |
Reads from the file into a memory buffer. More... | |
bool | ReadAll (wxString *str, const wxMBConv &conv=wxConvAuto()) |
Reads the entire contents of the file into a string. More... | |
wxFileOffset | Seek (wxFileOffset ofs, wxSeekMode mode=wxFromStart) |
Seeks to the specified position. More... | |
wxFileOffset | SeekEnd (wxFileOffset ofs=0) |
Moves the file pointer to the specified number of bytes relative to the end of the file. More... | |
wxFileOffset | Tell () const |
Returns the current position or wxInvalidOffset if file is not opened or if another error occurred. More... | |
size_t | Write (const void *buffer, size_t count) |
Write data to the file (descriptor). More... | |
bool | Write (const wxString &s, const wxMBConv &conv=wxConvAuto()) |
Writes the contents of the string to the file, returns true on success. More... | |
int | fd () const |
Returns the file descriptor associated with the file. More... | |
Static Public Member Functions | |
static bool | Access (const wxString &name, wxFile::OpenMode mode) |
This function verifies if we may access the given file in specified mode. More... | |
static bool | Exists (const wxString &filename) |
Returns true if the given name specifies an existing regular file (not a directory or a link). More... | |
enum wxFile::OpenMode |
The OpenMode enumeration defines the different modes for opening a file with wxFile.
It is also used with wxFile::Access function.
Enumerator | |
---|---|
read | Open file for reading or test if it can be opened for reading with Access() |
write | Open file for writing deleting the contents of the file if it already exists or test if it can be opened for writing with Access(). |
read_write | Open file for reading and writing; cannot be used with Access() |
write_append | Open file for appending: the file is opened for writing, but the old contents of the file are not erased and the file pointer is initially placed at the end of the file; cannot be used with Access(). This is the same as OpenMode::write if the file doesn't exist. |
write_excl | Open the file securely for writing (Uses O_EXCL | O_CREAT). Will fail if the file already exists, else create and open it atomically. Useful for opening temporary files without being vulnerable to race exploits. |
wxFile::wxFile | ( | ) |
Default constructor.
wxFile::wxFile | ( | const wxString & | filename, |
wxFile::OpenMode | mode = wxFile::read |
||
) |
Opens a file with a filename.
filename | The filename. |
mode | The mode in which to open the file. |
wxFile::wxFile | ( | int | fd | ) |
Associates the file with the given file descriptor, which has already been opened.
See Attach() for the list of predefined descriptors.
fd | An existing file descriptor. |
wxFile::~wxFile | ( | ) |
Destructor will close the file.
|
static |
This function verifies if we may access the given file in specified mode.
Only values of wxFile::read
or wxFile::write
really make sense here.
void wxFile::Attach | ( | int | fd | ) |
Attaches an existing file descriptor to the wxFile object.
Examples of predefined file descriptors are 0, 1 and 2 which correspond to stdin, stdout and stderr (and have symbolic names of wxFile::fd_stdin
, wxFile::fd_stdout
and wxFile::fd_stderr
).
The descriptor should be already opened and it will be closed by wxFile object.
void wxFile::ClearLastError | ( | ) |
bool wxFile::Close | ( | ) |
Closes the file.
bool wxFile::Create | ( | const wxString & | filename, |
bool | overwrite = false , |
||
int | access = wxS_DEFAULT |
||
) |
Creates a file for writing.
If the file already exists, setting overwrite to true will ensure it is overwritten.
access may be an OR combination of the wxPosixPermissions enumeration values.
int wxFile::Detach | ( | ) |
Get back a file descriptor from wxFile object - the caller is responsible for closing the file if this descriptor is opened.
IsOpened() will return false after call to Detach().
bool wxFile::Eof | ( | ) | const |
Returns true if the end of the file has been reached.
Note that the behaviour of the file pointer-based class wxFFile is different as wxFFile::Eof() will return true here only if an attempt has been made to read past the last byte of the file, while wxFile::Eof() will return true even before such attempt is made if the file pointer is at the last position in the file.
Note also that this function doesn't work on unseekable file descriptors (examples include pipes, terminals and sockets under Unix) and an attempt to use it will result in an error message.
So, to read the entire file into memory, you should write a loop which uses Read() repeatedly and tests its return condition instead of using Eof() as this will not work for special files under Unix.
|
static |
Returns true if the given name specifies an existing regular file (not a directory or a link).
int wxFile::fd | ( | ) | const |
Returns the file descriptor associated with the file.
bool wxFile::Flush | ( | ) |
Flushes the file descriptor.
Note that Flush() is not implemented on some Windows compilers due to a missing fsync function, which reduces the usefulness of this function (it can still be called but it will do nothing on unsupported compilers).
wxFileKind wxFile::GetKind | ( | ) | const |
Returns the type of the file.
int wxFile::GetLastError | ( | ) | const |
Returns the error code for the last unsuccessful operation.
The error code is system-dependent and corresponds to the value of the standard errno
variable when the last error occurred.
Notice that only simple accessors such as IsOpened() and Eof() (and this method itself) don't modify the last error value, all other methods can potentially change it if an error occurs, including the const ones such as Tell() or Length().
bool wxFile::IsOpened | ( | ) | const |
Returns true if the file has been opened.
wxFileOffset wxFile::Length | ( | ) | const |
Returns the length of the file.
bool wxFile::Open | ( | const wxString & | filename, |
wxFile::OpenMode | mode = wxFile::read , |
||
int | access = wxS_DEFAULT |
||
) |
Opens the file, returning true if successful.
filename | The filename. |
mode | The mode in which to open the file. |
access | An OR-combination of wxPosixPermissions enumeration values. |
ssize_t wxFile::Read | ( | void * | buffer, |
size_t | count | ||
) |
Reads from the file into a memory buffer.
buffer | Buffer to write in |
count | Bytes to read |
bool wxFile::ReadAll | ( | wxString * | str, |
const wxMBConv & | conv = wxConvAuto() |
||
) |
Reads the entire contents of the file into a string.
Since wxWidgets 3.1.1 this method also works for unseekable files.
str | Non-nullptr pointer to a string to read data into. |
conv | Conversion object to use in Unicode build; by default supposes that file contents is encoded in UTF-8 but falls back to the current locale encoding (or Latin-1 if it is UTF-8 too) if it is not. |
wxFileOffset wxFile::Seek | ( | wxFileOffset | ofs, |
wxSeekMode | mode = wxFromStart |
||
) |
Seeks to the specified position.
ofs | Offset to seek to. |
mode | One of wxFromStart, wxFromEnd, wxFromCurrent. |
wxFileOffset wxFile::SeekEnd | ( | wxFileOffset | ofs = 0 | ) |
Moves the file pointer to the specified number of bytes relative to the end of the file.
For example, SeekEnd
(-5) would position the pointer 5 bytes before the end.
ofs | Number of bytes before the end of the file. |
wxFileOffset wxFile::Tell | ( | ) | const |
Returns the current position or wxInvalidOffset if file is not opened or if another error occurred.
size_t wxFile::Write | ( | const void * | buffer, |
size_t | count | ||
) |
Write data to the file (descriptor).
buffer | Buffer from which to read data |
count | Number of bytes to write |
bool wxFile::Write | ( | const wxString & | s, |
const wxMBConv & | conv = wxConvAuto() |
||
) |
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 a multibyte representation.
Note that this method only works with NUL-terminated
strings, if you want to write data with embedded NULs
to the file you should use the other Write() overload.