Click here to Skip to main content
15,667,577 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
Hi everybody? I want to find a certain way to delete a file? some files cannot be deleted in a simple way with remove commands. like malwares or opened files. Is there any command that can do force delete?
Updated 9-Aug-22 8:09am
Hamed Moezzi Azimi 5-Nov-13 13:08pm    
I think the answer is in this link:

To remove a file in C++ use the remove function from cstdio.
#include <cstdio>
int main() {

    const int result = remove("C:\\Temp\\somefile.txt");
    return 0;

If this fails, as indicated by the result, that means the file either didn't exist, the file was read-only or the file was owned by a user different than the one executing the command.

If the file is read-only you can try first changing this (this will also fail if the current user doens't have rights to do so), but there's is no C++ "native" way of changing file attributes so you'll have to use something local to the installation your on.
On Windows for example, try using the SetFileAttributes method found in windows.h (link against Kernel32).

If the current user does not have rights to delete the file, the you must make sure the application is run as either the user who owns the file or as an administrator who can delete it.

Also, note that some malware may use a secondary process to recreate files and processes as you kill/delete them, this is a lot harder to deal with and is not a C++ specific problem.

Hope this helps,
Share this answer
nv3 4-Nov-13 4:32am    
My 5.
you can use the remove function
#include <<cstdio>>>
int remove(char *filename)
file name is path of file with name of file
Share this answer
If you are on Win32, the official way to do it is to mark it to be deleted on reboot, and ask the user to reboot. To mark the file to be deleted on reboot, use MoveFileEx with the MOVEFILE_DELAY_UNTIL_REBOOT flag (pass NULL as the destination).
good luck
Share this answer
1: first you needs to get all opening processes by using OpenProcess() function
2: check for process "notepad"
3: then kill that process and use remove(pathname) function to delete opening file
Share this answer
There is in
some different ways to manage the problem that you can select due to your exact problem because it may be initiated by different roots.
Share this answer
"remove, _wremove
Delete a file.

int remove( const char *path );

int _wremove( const wchar_t *path );
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900