Version: 2.9.4
Public Types | Public Member Functions | Static Public Member Functions
wxFile Class Reference

#include </home/zeitlin/src/wx/github/interface/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.
 wxFile (const wxString &filename, wxFile::OpenMode mode=wxFile::read)
 Opens a file with a filename.
 wxFile (int fd)
 Associates the file with the given file descriptor, which has already been opened.
 ~wxFile ()
 Destructor will close the file.
int GetLastError () const
 Returns the error code for the last unsuccessful operation.
void ClearLastError ()
 Resets the error code.
void Attach (int fd)
 Attaches an existing file descriptor to the wxFile object.
bool Close ()
 Closes the file.
bool Create (const wxString &filename, bool overwrite=false, int access=wxS_DEFAULT)
 Creates a file for writing.
void Detach ()
 Get back a file descriptor from wxFile object - the caller is responsible for closing the file if this descriptor is opened.
bool Eof () const
 Returns true if the end of the file has been reached.
bool Flush ()
 Flushes the file descriptor.
wxFileKind GetKind () const
 Returns the type of the file.
bool IsOpened () const
 Returns true if the file has been opened.
wxFileOffset Length () const
 Returns the length of the file.
bool Open (const wxString &filename, wxFile::OpenMode mode=wxFile::read, int access=wxS_DEFAULT)
 Opens the file, returning true if successful.
ssize_t Read (void *buffer, size_t count)
 Reads from the file into a memory buffer.
wxFileOffset Seek (wxFileOffset ofs, wxSeekMode mode=wxFromStart)
 Seeks to the specified position.
wxFileOffset SeekEnd (wxFileOffset ofs=0)
 Moves the file pointer to the specified number of bytes relative to the end of the file.
wxFileOffset Tell () const
 Returns the current position or wxInvalidOffset if file is not opened or if another error occurred.
size_t Write (const void *buffer, size_t count)
 ファイル (ディスクリプタ) へデータを書き込みます。
bool Write (const wxString &s, const wxMBConv &conv=wxConvUTF8)
 Writes the contents of the string to the file, returns true on success.
int fd () const
 Returns the file descriptor associated with the file.

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.
static bool Exists (const wxString &filename)
 Returns true if the given name specifies an existing regular file (not a directory or a link).

List of all members.


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.

注:
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.

void 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 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
参照:
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.
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.

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.

size_t wxFile::Write ( const void *  buffer,
size_t  count 
)

ファイル (ディスクリプタ) へデータを書き込みます。

Parameters:
bufferBuffer from which to read data
countNumber of bytes to write
Returns:
The number of bytes written.
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines