Creating and deleting a temp file
-
I want to create a temp file do some process and delete it. I've create a temp file like this.
szTempFile = new TCHAR[MAX_PATH]; if(!::GetTempFileName(".","temp",0,szTempFile)) { return -1; }
It's creating a temp file on the current working folder. Then I try to delete the file as follows.if(szTempFile != NULL) { //delete szTempFile; HRESULT b = ::DeleteFile(szTempFile); }
This not delete the file. When I check the path it's valid but the result of parameter b is 0. Can someone help me to find the error.I appreciate your help all the time... CodingLover :)
-
I want to create a temp file do some process and delete it. I've create a temp file like this.
szTempFile = new TCHAR[MAX_PATH]; if(!::GetTempFileName(".","temp",0,szTempFile)) { return -1; }
It's creating a temp file on the current working folder. Then I try to delete the file as follows.if(szTempFile != NULL) { //delete szTempFile; HRESULT b = ::DeleteFile(szTempFile); }
This not delete the file. When I check the path it's valid but the result of parameter b is 0. Can someone help me to find the error.I appreciate your help all the time... CodingLover :)
Call
GetLastError
afterDeleteFile
and check what it returns.«_Superman_» I love work. It gives me something to do between weekends.
-
Call
GetLastError
afterDeleteFile
and check what it returns.«_Superman_» I love work. It gives me something to do between weekends.
Yes, I got the error code as 32. I'm mess with the finding what it means.
I appreciate your help all the time... CodingLover :)
-
Yes, I got the error code as 32. I'm mess with the finding what it means.
I appreciate your help all the time... CodingLover :)
It means the file is in use. You can delete the file only after it is closed using
CloseHandle
.«_Superman_» I love work. It gives me something to do between weekends.
-
I want to create a temp file do some process and delete it. I've create a temp file like this.
szTempFile = new TCHAR[MAX_PATH]; if(!::GetTempFileName(".","temp",0,szTempFile)) { return -1; }
It's creating a temp file on the current working folder. Then I try to delete the file as follows.if(szTempFile != NULL) { //delete szTempFile; HRESULT b = ::DeleteFile(szTempFile); }
This not delete the file. When I check the path it's valid but the result of parameter b is 0. Can someone help me to find the error.I appreciate your help all the time... CodingLover :)
-
It means the file is in use. You can delete the file only after it is closed using
CloseHandle
.«_Superman_» I love work. It gives me something to do between weekends.
Can you bit explain how to use a handle for the file. I just use TCHAR buffer to hold the path. I hope your clear with my previous code segment.
I appreciate your help all the time... CodingLover :)
-
As Superman says, closing the file? Can you please explain a bit more how to do it. Actually I don't have handler to the file, in my code posted before.
I appreciate your help all the time... CodingLover :)
-
As Superman says, closing the file? Can you please explain a bit more how to do it. Actually I don't have handler to the file, in my code posted before.
I appreciate your help all the time... CodingLover :)
-
I use SHCreateStreamOnFile stream on the file. Something like this.
if (FAILED(hr = SHCreateStreamOnFile(szTempFile, STGM_CREATE | STGM_WRITE, &pOutFileStream)))
I appreciate your help all the time... CodingLover :)
-
I want to create a temp file do some process and delete it. I've create a temp file like this.
szTempFile = new TCHAR[MAX_PATH]; if(!::GetTempFileName(".","temp",0,szTempFile)) { return -1; }
It's creating a temp file on the current working folder. Then I try to delete the file as follows.if(szTempFile != NULL) { //delete szTempFile; HRESULT b = ::DeleteFile(szTempFile); }
This not delete the file. When I check the path it's valid but the result of parameter b is 0. Can someone help me to find the error.I appreciate your help all the time... CodingLover :)
I wonder if Windows is expecting you to call CreateFile on the filename before you delete it? Have you looked at http://msdn.microsoft.com/en-us/library/aa363875(VS.85).aspx[^] which gives an example of creating and using a temporary file?
-
Can you bit explain how to use a handle for the file. I just use TCHAR buffer to hold the path. I hope your clear with my previous code segment.
I appreciate your help all the time... CodingLover :)
What are you doing after creating the temporary file? Are you opening it using
fopen
orCreateFile
? What operations are you doing with the file?«_Superman_» I love work. It gives me something to do between weekends.
-
I use SHCreateStreamOnFile stream on the file. Something like this.
if (FAILED(hr = SHCreateStreamOnFile(szTempFile, STGM_CREATE | STGM_WRITE, &pOutFileStream)))
I appreciate your help all the time... CodingLover :)
That stream creation call will be opening the file. Then you'll need to make sure you call
pOutFileStream->Release ()
at a convenient moment. Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...
-
What are you doing after creating the temporary file? Are you opening it using
fopen
orCreateFile
? What operations are you doing with the file?«_Superman_» I love work. It gives me something to do between weekends.
I read that you're using
SHCreateStreamOnFile
to open the file. So you must callpOutFileStream->Release()
before you can callDeleteFile
.«_Superman_» I love work. It gives me something to do between weekends.
-
I read that you're using
SHCreateStreamOnFile
to open the file. So you must callpOutFileStream->Release()
before you can callDeleteFile
.«_Superman_» I love work. It gives me something to do between weekends.
Yes I've already close the stream before delete the file. What I'm doing is, create a temp file, then write some data to it. And then read the content into ostream and use that stream for processing. I want to delete the temp file. When I check the temp file path it's on working folder, like this.
.\ad.tmp
It wont be a case right?
I appreciate your help all the time... CodingLover :)
-
Yes I've already close the stream before delete the file. What I'm doing is, create a temp file, then write some data to it. And then read the content into ostream and use that stream for processing. I want to delete the temp file. When I check the temp file path it's on working folder, like this.
.\ad.tmp
It wont be a case right?
I appreciate your help all the time... CodingLover :)
Thanks a lot my friends. I've solve my problem. Actually I didn't properly close one of stream handler in the process. Thanks again.
I appreciate your help all the time... CodingLover :)