wxtempfilewxtempfile 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: members
wxtempfile::wxtempfile
wxtempfile::wxtempfilewxtempfile() default constructor - open must be used to open the file.
wxtempfile::wxtempfilewxtempfile(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::openbool 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::isopenedbool isopened() const returns true if the file was successfully opened.
wxtempfile::lengthwxfileoffset length() const returns the length of the file.
wxtempfile::seekwxfileoffset seek(wxfileoffset ofs, wxseekmode mode = wxfromstart) seeks to the specified position.
wxtempfile::tellwxfileoffset tell() const returns the current position or wxinvalidoffset if file is not opened or if another error occurred.
wxtempfile::writebool write(const void *p, size_t n) write to the file, return true on success, false on failure.
wxtempfile::writebool 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::commitbool 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::discardvoid 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.
|