Reputation:
I have implemented the following function in which it gets the path of a file and then tries to delete it from the file system.
The function doesn't work properly, because when I call it to remove a file, the file will not be deleted until I close the program. When I close the program, the file that I specified will be deleted. I don't know what is wrong with the code:
bool DeleteFileOnSystem(const char* arg_path, const char* arg_name_file)
{
char c_FilePath[MAX_PATH];
strcpy_s(c_FilePath, MAX_PATH, arg_path);
strcat(c_FilePath, arg_name_file);
if (DeleteFile(c_FilePath))
return true;
else
return false;
}
Upvotes: 0
Views: 822
Reputation: 2839
When you open DeleteFile documentation, you can find following statement:
The DeleteFile function marks a file for deletion on close. Therefore, the file deletion does not occur until the last handle to the file is closed. Subsequent calls to CreateFile to open the file fail with ERROR_ACCESS_DENIED.
Make sure all the handles to the file are closed before you call the DeleteFile
API.
Upvotes: 2
Reputation: 13679
It does delete file and nothing is wrong.
Apparently your program opens file somewhere else and does not close it. So the handle stays open until program exists.
Make sure you close your file handles.
This behavior of DeleteFile
is a feature, not a bug.
Upvotes: 1