Temporary Files | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||
Download: |
|
|||||||||||||||||
This file helps generate a suitable temporary file name and ensure it is deleted after use. |
CAutoDeletePath is an RAII object derived from CString which, if it's holding a path to a file or folder, will delete that file or folder when the CAutoDeletePath goes out of scope.
CTempPath creates a unique Temporary File Name from a Template.
The file can optionally be AutoDeleted when the CTempPath goes out of scope
The printf-style Template should use an integer parameter (%04d):
Save(CTempPath("PXBF%04d.X_B"));
This would produce a file-name like:
"C:\DOCUME~1\rjm\LOCALS~1\Temp\PXBF0021.X_B"
You must provide a template to describe the whole File Name, or it can't know if it's generating a duplicate!ValidateTempFolder makes sure a temporary folder exists and is writeable (yes, I've had customers who had no access to their system temp folder).
It's worth pointing out that Windows has a flag specifically for making temporary files fast: FILE_ATTRIBUTE_TEMPORARY. If you use CreateFile with this flag, the file isn't flushed to the disk unless you run out of RAM, so if you're writing a small temp file and about to read it back in elsewhere, this is a fast option. It has been used where several third-party software modules were working on file data sequentially.
CTempPath oPath("MyApp%04d.tmp"); HANDLE oFile=CreateFile(oPath, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_TEMPORARY, 0); if(oFile) { ...fill the temp file CloseHandle(oFile); ...now some other process can open it and use it without it having been written to disk. }You can also use the flag FILE_FLAG_DELETE_ON_CLOSE instead of using CTempPath's AutoDelete if you are using CreateFile.
TempPath.h uses LocalPath.h
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.