Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <string>
- using namespace std;
- int main (void){
- string filename;
- ifstream file;
- // Some bool variables for the do while input loop
- bool isvalid = true; // Will change to false if failing the first loop
- bool isopened = false;
- bool failedopen = false;
- do {
- if ( failedopen ){
- cout << "There was an error opening the file." << endl;
- }
- isvalid = true; // Reset for each loop
- cout << "Enter filename: ";
- cin >> filename;
- isvalid = validFilename(filename);
- // If the filename is valid, try to open the file
- if(isvalid){
- file.open(filename);
- }
- if ( file.fail() ){
- // File was not opened
- isopened = false;
- }
- else {
- // File was opened;
- isopened = true;
- }
- }
- } while ( !isvalid || !isopened );
Add Comment
Please, Sign In to add comment