Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Deleting Temporary Files

Posted on 2000-04-26
Medium Priority
Last Modified: 2008-01-09

How automaticly delete temporary files, after the program closed ?

Question by:masvmasv
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Accepted Solution

jkr earned 200 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,
                                dwFlags | FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_SEQUENTIAL_SCAN,
LVL 22

Expert Comment

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?

LVL 86

Expert Comment

ID: 2752794
>>In windows you can open the file with

Nice flag, but wrong effect ;-)

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."
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 22

Expert Comment

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....)

Author Comment

ID: 2753304

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

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


LVL 22

Expert Comment

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.
LVL 86

Expert Comment

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.

LVL 22

Expert Comment

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.  

Expert Comment

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.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

722 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