Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1099
  • Last Modified:

Deleting Temporary Files

Hi...

How automaticly delete temporary files, after the program closed ?

Thanks
0
masvmasv
Asked:
masvmasv
1 Solution
 
jkrCommented:
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
 
nietodCommented:
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
 
jkrCommented:
>>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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nietodCommented:
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
 
masvmasvAuthor Commented:
Hi..

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

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

thanks

0
 
nietodCommented:
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
 
jkrCommented:
>>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
 
nietodCommented:
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
 
abesoftCommented:
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now