Solved

Deleting Temporary Files

Posted on 2000-04-26
9
1,087 Views
Last Modified: 2008-01-09
Hi...

How automaticly delete temporary files, after the program closed ?

Thanks
0
Comment
Question by:masvmasv
9 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 2752770
There's no automatic way of doing that. If you're using Win32 (which I assume), you can pass a flag called 'FILE_FLAG_DELETE_ON_CLOSE' to 'CreateFile()' to automate this process, e.g.

    hFile   =   CreateFile  (   "myfile.tmp",
                                GENERIC_WRITE | GENERIC_READ,
                                FILE_SHARE_READ | FILE_SHARE_WRITE,
                                &sa,
                                CREATE_ALWAYS,
                                dwFlags | FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_SEQUENTIAL_SCAN,
                                NULL
                            );
0
 
LVL 22

Expert Comment

by:nietod
ID: 2752771
In windows you can open the file with the FILE_ATTRIBUTE_TEMPORARY bit flag.  This will cause the OS to delete the file automatically when it is closed.

Other OS may have this sort of feature, but probably not.

Other than that, you just have to delete the file manually before the program finishes.

What OS are you using?

0
 
LVL 86

Expert Comment

by:jkr
ID: 2752794
>>In windows you can open the file with
>>the FILE_ATTRIBUTE_TEMPORARY bit flag

Nice flag, but wrong effect ;-)

"FILE_ATTRIBUTE_TEMPORARY
The file is being used for temporary storage. File systems attempt to keep all of the data in memory for quicker access rather than flushing the data back to mass storage. A temporary file should be deleted by the application as soon as it is no longer needed."
0
 
LVL 22

Expert Comment

by:nietod
ID: 2752835
Opps.  it should have been FILE_FLAG_DELETE_ON_CLOSE  (which in my program I have set to a constant that uses "temporary" in the name....)
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:masvmasv
ID: 2753304
Hi..

Thanks...
But the files was created with fopen(... "wb" );  function...

I need compatiblity with Windows 9x, NT and 2000....

thanks

0
 
LVL 22

Expert Comment

by:nietod
ID: 2753718
There is no "direct" way to do this from standard C++.  You just have to delete the files yourself (using the DeleteFile() windowws API function.)
You could use classes to help automate this.  Like classes that delete the file in the destructor.  This sort of approach would even allow the file to be deleted when the program throws an exception.
0
 
LVL 86

Expert Comment

by:jkr
ID: 2755058
>>I need compatiblity with Windows 9x,
>>NT and 2000....

If so, 'CreateFile()' is even the better choice. If you don't want to change too much of your code, I'd suggest using sth. like

HANDLE hFile = CreateFile ( ...);
FILE*  fp = _open_osfhandle ( ( long) hFile, _O_TEXT);

so you'll still be able to use the file with the CRT functions.



0
 
LVL 22

Expert Comment

by:nietod
ID: 2755101
That's nice.  Best of both worlds.  Actualy it would be best if you could get an fstream to use the handle, but that doesn't seem too feasible, not without writting a stream buffer class.  
0
 
LVL 2

Expert Comment

by:abesoft
ID: 2755673
Actually, the code posted should read:

    // Create the file as a HANDLE
    HANDLE hFile = CreateFile( ...);
    DWORD garbage;
#define M1 "This is a Handle\n"
    WriteFile( hFile, M1, strlen( M1), &garbage, NULL);

    // Now, turn the HANDLE into an fd
    int os_handle =  _open_osfhandle ( (long) hFile, _O_TEXT);
#define MESSAGE "This is a _write\n"
    _write( os_handle, MESSAGE, strlen( MESSAGE));

    // And turn the FD into a FILE *
    FILE *fp = _fdopen( os_handle, "rw");
    fprintf( fp, "This is the FILE\n");

    // Or turn the fd into an fstream
    fstream file;
    file.attach( os_handle);
    file << "This is a stream" << endl;

Obviously, this code doesn't do any of the close operations.  Another excercise for the reader.

The earlier posting used:
FILE*  fp = _open_osfhandle ( ( long) hFile, _O_TEXT);
which doesn't compile without casts, and also didn't run.  jkr had the right idea, though.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now