## wxFile

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 making it unnecessary to worry about forgetting to do it. 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 if a typedef for a native 64 bit integer, else a 32 bit integer is used for wxFileOffset.

Derived from

None.

Include files

<wx/file.h>

Constants

wx/file.h defines the following constants:

    #define wxS_IRUSR 00400
    #define wxS_IWUSR 00200
    #define wxS_IXUSR 00100

    #define wxS_IRGRP 00040
    #define wxS_IWGRP 00020
    #define wxS_IXGRP 00010

    #define wxS_IROTH 00004
    #define wxS_IWOTH 00002
    #define wxS_IXOTH 00001

    // default mode for the new files: corresponds to umask 022
    #define wxS_DEFAULT  (wxS_IRUSR | wxS_IWUSR | wxS_IRGRP | wxS_IWGRP | wxS_IROTH | wxS_IWOTH)

These constants define the file access rights and are used with wxFile::Create and wxFile::Open.

The OpenMode enumeration defines the different modes for opening a file, it is defined inside wxFile class so its members should be specified with wxFile:: scope resolution prefix. It is also used with wxFile::Access function.

wxFile::read | Open file for reading or test if it can be opened for reading with Access()
wxFile::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()
wxFile::read_write | Open file for reading and writing; can not be used with Access()
wxFile::write_append | Open file for appending: the file is opened for writing, but the old contents of the file is not erased and the file pointer is initially placed at the end of the file; can not be used with Access(). This is the same as wxFile::write if the file doesn't exist.
wxFile::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.

Other constants defined elsewhere but used by wxFile functions are wxInvalidOffset which represents an invalid value of type wxFileOffset and is returned by functions returning wxFileOffset on error and the seek mode constants used with Seek():

wxFromStart | Count offset from the start of the file
wxFromCurrent | Count offset from the current position of the file pointer
wxFromEnd | Count offset from the end of the file (backwards)

Members

wxFile::wxFile
wxFile::~wxFile
wxFile::Access
wxFile::Attach
wxFile::Close
wxFile::Create
wxFile::Detach
wxFile::fd
wxFile::Eof
wxFile::Exists
wxFile::Flush
wxFile::GetKind
wxFile::IsOpened
wxFile::Length
wxFile::Open
wxFile::Read
wxFile::Seek
wxFile::SeekEnd
wxFile::Tell
wxFile::Write
wxFile::Write

### wxFile::wxFile

wxFile()

Default constructor.

wxFile(const char* filename, wxFile::OpenMode mode = wxFile::read)

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 fail.

wxFile(int fd)

Associates the file with the given file descriptor, which has already been opened.

Parameters

filename

The filename.

mode

The mode in which to open the file. May be one of wxFile::read, wxFile::write and wxFile::read_write.

fd

An existing file descriptor (see Attach() for the list of predefined descriptors)

### wxFile::~wxFile

~wxFile()

Destructor will close the file.

NB: it is not virtual so you should not use wxFile polymorphically.

### wxFile::Access

static bool Access(const char * name, OpenMode mode)

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.

### wxFile::Attach

void Attach(int fd)

Attaches an existing file descriptor to the wxFile object. Example 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.

### wxFile::Close

void Close()

Closes the file.

### wxFile::Create

bool Create(const char* 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.

### wxFile::Detach

void 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().

### wxFile::fd

int fd() const

Returns the file descriptor associated with the file.

### wxFile::Eof

bool 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 in such case. 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.

### wxFile::Exists

static bool Exists(const char* filename)

Returns true if the given name specifies an existing regular file (not a directory or a link)

### wxFile::Flush

bool Flush()

Flushes the file descriptor.

Note that wxFile::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).

### wxFile::GetKind

wxFileKind GetKind() const

Returns the type of the file. Possible return values are:

    enum wxFileKind
    {
      wxFILE_KIND_UNKNOWN,
      wxFILE_KIND_DISK,     // a file supporting seeking to arbitrary offsets
      wxFILE_KIND_TERMINAL, // a tty
      wxFILE_KIND_PIPE      // a pipe
    };

### wxFile::IsOpened

bool IsOpened() const

Returns true if the file has been opened.

### wxFile::Length

wxFileOffset Length() const

Returns the length of the file.

### wxFile::Open

bool Open(const char* filename, wxFile::OpenMode mode = wxFile::read)

Opens the file, returning true if successful.

Parameters

filename

The filename.

mode

The mode in which to open the file. May be one of wxFile::read, wxFile::write and wxFile::read_write.

### wxFile::Read

size_t Read(void* buffer, size_t count)

Reads the specified number of bytes into a buffer, returning the actual number read.

Parameters

buffer

A buffer to receive the data.

count

The number of bytes to read.

Return value

The number of bytes read, or the symbol wxInvalidOffset (-1) if there was an error.

### wxFile::Seek

wxFileOffset Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart)

Seeks to the specified position.

Parameters

ofs

Offset to seek to.

mode

One of wxFromStart, wxFromEnd, wxFromCurrent.

Return value

The actual offset position achieved, or wxInvalidOffset on failure.

### wxFile::SeekEnd

wxFileOffset SeekEnd(wxFileOffset ofs = 0)

Moves the file pointer to the specified number of bytes before the end of the file.

Parameters

ofs

Number of bytes before the end of the file.

Return value

The actual offset position achieved, or wxInvalidOffset on failure.

### wxFile::Tell

wxFileOffset Tell() const

Returns the current position or wxInvalidOffset if file is not opened or if another error occurred.

### wxFile::Write

size_t Write(const void* buffer, wxFileOffset count)

Writes the specified number of bytes from a buffer.

Parameters

buffer

A buffer containing the data.

count

The number of bytes to write.

Return value

the number of bytes actually written

### wxFile::Write

bool Write(const wxString& s, wxMBConv& conv = wxConvUTF8)

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.

Note that this method only works with `NUL`-terminated strings, if you want to write data with embedded `NUL`s to the file you should use the other Write() overload.