Is there a function to check if a file exists?
Not so much a function, but a way
Say you are trying to open a file to write to
you'd need to #include <fstream>
then to open the file use:
but you always have to check if that file actually exists, or else it's be useless. soCode:ofstream myFile("local location of file");
is that what you wanted? i imagine you could just use that even if you didn't want to write to the file.Code:if (! myFile) { cout << "Error\n"; return -1; }
You could also use the WIN API call "_stat()" to check on file status. it returns a -1 on error / file not found
Code:int _stat( const char *path, struct _stat *buffer );
Never heard of the WIN API. How do I add that to my code?
Frantic, if youve never heard of it, then just stick to standard c++
Code:#include <fstream> void main( void ) { ifstream fin("myfile.ini" , ios::in ); ofstream fout; if( fin.fail() ) //File error { //So create the file fout.open("myfile.ini" , ios::out | ios::trunc ); //File it with the default text/binary fout << "Whateveryouwantasdefault"; //Close and clear the file write buffer fout.close(); fout.clear(); //And reopen the file fin.open("myfile.ini" , ios::in ); } //Now do your normal operations as if nothing ever happend fin.read((char *)&bla , sizeof(blastruct)); fin.close(); }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks