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,
Fredrik