contents up previous next

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 is a typedef for a native 64 bit integer, otherwise 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

mode

fd


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

mode


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

count

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

mode

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 relative to the end of the file. for example, seekend(-5) would position the pointer 5 bytes before the end.

parameters

ofs

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, size_t count)

writes the specified number of bytes from a buffer.

parameters

buffer

count

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 nuls to the file you should use the other write() overload.