/ Published in: C++
Just a lazy way to verify file exists before reading from another function. It may not be the best so feel free to comment on better ways. Just create a string in function you are calling from or main and do this:
string getData, file; ifstream inData; file = checkFileExists(); inData.open(file.c_str()); inData >> getData;
Expand |
Embed | Plain Text
string checkFileExists(){ string file; ifstream inputFile; cout << "Enter file name: " << endl; cin >> file; inputFile.open(file.c_str()); if(inputFile){ return file; } else{ cout << "ERROR: not found!" << endl; file.clear(); file = checkFileExists(); } inputFile.close(); inputFile.clear(); }
You need to login to post a comment.
