back to io 

wxTempFile

generated from ../src/mod_io/tempfile.cpp

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.

Constants

Constructor

wxTempFile

wxTempFile()
 
wxTempFile(FileName)
 
Name Type Default Description
FileName String The filename

Constructs a new wxTempFile object. When a filename is passed, The file is opened, and you can check whether the operation was successful or not by checking opened.

Properties

Name Type Description
length read only Integer Returns the length of the file. When the file is not open, undefined is returned.
opened read only Boolean Returns true when the file is open.
tell read only Integer Returns the current position or -1. When the file is not open, undefined is returned.

Methods

commit

commit() : Boolean
 

Validate changes: deletes the old file 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.

discard

discard()
 

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

open

open(FileName) : Boolean
 
Name Type Default Description
FileName String The name of the file

Open the temporary file, returns true on success, false if an error occurred. FileName is the name of file to be replaced. The temporary file is always created in the directory where FileName is. In particular, if FileName 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.

seek

seek(Offset, 
     Mode = wxFile.FromStart) : Integer
 
Name Type Default Description
Offset Integer Offset to seek to.
Mode wxSeekMode wxFile.FromStart

Seeks the offset. Returns the actual position or -1. See also seekEnd.

write

write(Buffer) : Integer
 
Name Type Default Description
Buffer Buffer
write(Str) : Integer
 
Name Type Default Description
Str String

Writes the string or buffer to the file. Returns the actual number of bytes written to the file.



SourceForge.net Logo Support This Project

Table of Contents

Constructor

wxTempFile

Properties

length
opened
tell

Methods

commit
discard
open
seek
write


Design downloaded from Zeroweb.org: Free website templates, layouts, and tools.