Version: 3.1.1
wxFile Class Reference

#include <wx/file.h>

Detailed Description

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.

Library:  wxBase
Category:  File Handling

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=wxConvUTF8)
 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...
 

Member Enumeration Documentation

anonymous enum

Standard file descriptors.

Enumerator
fd_invalid 
fd_stdin 
fd_stdout 
fd_stderr 

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.

Constructor & Destructor Documentation

wxFile::wxFile ( )

Default constructor.

wxFile::wxFile ( const wxString filename,
wxFile::OpenMode  mode = wxFile::read 
)

Opens a file with a filename.

Parameters
filenameThe filename.
modeThe mode in which to open the file.
Warning
You should use IsOpened() to verify that the constructor succeeded.
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.

Parameters
fdAn existing file descriptor.
wxFile::~wxFile ( )

Destructor will close the file.

Note
This destructor is not virtual so you should not use wxFile polymorphically.

Member Function Documentation

static bool wxFile::Access ( const wxString name,
wxFile::OpenMode  mode 
)
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 ( )

Resets the error code.

GetLastError() will return 0 until the next error occurs.

Since
2.9.2
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().

Returns
The file descriptor (this is new since wxWidgets 3.0.0, in the previous versions this method didn't return anything).
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 bool wxFile::Exists ( const wxString filename)
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().

Since
2.9.2
See also
ClearLastError()
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.

Parameters
filenameThe filename.
modeThe mode in which to open the file.
accessAn OR-combination of wxPosixPermissions enumeration values.
ssize_t wxFile::Read ( void *  buffer,
size_t  count 
)

Reads from the file into a memory buffer.

Parameters
bufferBuffer to write in
countBytes to read
Returns
The number of bytes read, or the symbol wxInvalidOffset.
bool wxFile::ReadAll ( wxString str,
const wxMBConv conv = wxConvAuto() 
)

Reads the entire contents of the file into a string.

Parameters
strNon-NULL pointer to a string to read data into.
convConversion 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.
Returns
true if file was read successfully, false otherwise.
Since
2.9.5
wxFileOffset wxFile::Seek ( wxFileOffset  ofs,
wxSeekMode  mode = wxFromStart 
)

Seeks to the specified position.

Parameters
ofsOffset to seek to.
modeOne of wxFromStart, wxFromEnd, wxFromCurrent.
Returns
The actual offset position achieved, or wxInvalidOffset on failure.
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.

Parameters
ofsNumber of bytes before the end of the file.
Returns
The actual offset position achieved, or wxInvalidOffset on failure.
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).

Parameters
bufferBuffer from which to read data
countNumber of bytes to write
Returns
The number of bytes written.
bool wxFile::Write ( const wxString s,
const 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 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.