need help with file opening errors
hi! i have a question about file opening. i'm supposed to allow a user to enter filenames of 5 text files. here is a portion of my code.
Code:
do {
cout << "Enter filename(e.g abc.txt): ";
cin >> filename;
inFile.open(filename.c_str());
if(inFile.fail()) {
cout << "Error!\n";
}
else {
inFile >> f;
..so on...
file_count++;
}
inFile.close();
}while(file_count < 5);
it works fine if the file can be found everytime.
i thought i have closed my file after each round so it doesnt matter. but after typing an incorrect filename once the program will state that inFile failed even though i typed a valid filename.
anyone knows whats wrong with this?
also, i changed the extension of another file type to .txt and tried opening it within the program. this causes the program to hang. just wondering is there's a way to test if the file is really a .txt before opening it. my aim is for the program to handle all invalid files properly..
i thank all who are willing to help or even spend time reading about my problem. =)