yes
how do we detect if a file doesn't exist on a harddisk then we perform an action??
yes
how do we detect if a file doesn't exist on a harddisk then we perform an action??
There are several ways. One is to just open the file and if you get a return error code, then it was not there or there was a problem accessing it.
Code:fstream File("file.file", ios::in | ios::out); if (!File) cout << "Error Opening File";
More technically:
Code:ifstream inputFile( filename, ios::in | ios::binary | ios::nocreate ); if ( inputFile.fail()) //do whatever
Is there a way to tell if the file isn't there, or if it just can't be opened?
>Is there a way to tell if the file isn't there
Aside from trying to open it, no, not in a portable way. But usually if it can't be opened it's not there.
-Prelude
My best code is written with the delete key.
Now, if that were converted to clean C++ code instead of this awfull jibberish...Code:FILE * Tmp; Tmp = fopen("tmp.bat", "a"); fprintf("@echo off\ncls"); fprintf("if not exist MyFile.ext echo darn\n"); fprintf("if exist MyFile.ext echo yup\n"); fclose(Tmp); system("tmp.bat"); ...or.... FILE * Tmp; if ( ( Tmp = fopen("MyFile.ext", "r+") ) == 0) { printf("it doesn't exist"); } else { fprintf("it does exist"); }
The world is waiting. I must leave you now.