Contents Up Previous Next

wxTempFile

wxTempFile provides a relatively safe way to replace the contents of the existing file. The name is explained by the fact that it may be also used as just a temporary file if you don't replace the old file contents.

Usually, when a program replaces the contents of some file it first opens it for writing, thus losing all of the old data and then starts recreating it. This approach is not very safe because during the regeneration of the file bad things may happen: the program may find that there is an internal error preventing it from completing file generation, the user may interrupt it (especially if file generation takes long time) and, finally, any other external interrupts (power supply failure or a disk error) will leave you without either the original file or the new one.

wxTempFile addresses this problem by creating a temporary file which is meant to replace the original file - but only after it is fully written. So, if the user interrupts the program during the file generation, the old file won't be lost. Also, if the program discovers itself that it doesn't want to replace the old file there is no problem - in fact, wxTempFile will not replace the old file by default, you should explicitly call Commit to do it. Calling Discard explicitly discards any modifications: it closes and deletes the temporary file and leaves the original file unchanged. If you don't call neither of Commit() and Discard(), the destructor will call Discard() automatically.

To summarize: if you want to replace another file, create an instance of wxTempFile passing the name of the file to be replaced to the constructor (you may also use default constructor and pass the file name to Open). Then you can write to wxTempFile using wxFile-like functions and later call Commit() to replace the old file (and close this one) or call Discard() to cancel the modifications.

Derived from

No base class

Include files

<wx/file.h>

See also:

wxFile

Members

wxTempFile::wxTempFile
wxTempFile::wxTempFile
wxTempFile::Open
wxTempFile::IsOpened
wxTempFile::Write
wxTempFile::Write
wxTempFile::Commit
wxTempFile::Discard
wxTempFile::~wxTempFile


wxTempFile::wxTempFile

wxTempFile()

Default constructor - Open must be used to open the file.


wxTempFile::wxTempFile

wxTempFile(const wxString& strName)

Associates wxTempFile with the file to be replaced and opens it. You should use IsOpened to verify if the constructor succeeded.


wxTempFile::Open

bool Open(const wxString& strName)

Open the temporary file, returns true on success, false if an error occurred.

strName is the name of file to be replaced. The temporary file is always created in the directory where strName is. In particular, if strName doesn't include the path, it is created in the current directory and the program should have write access to it for the function to succeed.


wxTempFile::IsOpened

bool IsOpened() const

Returns true if the file was successfully opened.


wxTempFile::Write

bool Write(const void *p, size_t n)

Write to the file, return true on success, false on failure.


wxTempFile::Write

bool Write(const wxString& str, wxMBConv& conv = wxConvLibc)

Write to the file, return true on success, false on failure.

The second argument is only meaningful in Unicode build of wxWidgets when conv is used to convert str to multibyte representation.


wxTempFile::Commit

bool Commit()

Validate changes: deletes the old file of name m_strName and renames the new file to the old name. Returns true if both actions succeeded. If false is returned it may unfortunately mean two quite different things: either that either the old file couldn't be deleted or that the new file couldn't be renamed to the old name.


wxTempFile::Discard

void Discard()

Discard changes: the old file contents is not changed, temporary file is deleted.


wxTempFile::~wxTempFile

~wxTempFile()

Destructor calls Discard() if temporary file is still opened.