Solved

Deleting Temporary Files

Posted on 2000-04-26
9
1,089 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

863 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

20 Experts available now in Live!

Get 1:1 Help Now