Solved

Deleting Temporary Files

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

How automaticly delete temporary files, after the program closed ?

Thanks
0
Comment
Question by:masvmasv
[X]
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
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
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!

 
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
 

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

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!

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

752 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