file open problem
Nov 25, 2010 at 12:05am UTC
1 2 3 4 5 6 7 8 9
do {
cout << "Enter 'File.txt' or 'exit': " ;
cin >> filename;
if (filename == "quit" || filename == "exit" )break ;
file.open(filename.c_str(),ios::beg);
cout << filename << file << endl;
if (file.fail())
cout<<"Failed to open file." ;
this runs through ok the first time but when it loops back it refuses to open a new file. i do close the file before it loops back to the beginning i am not sure what wrong.
Nov 25, 2010 at 2:43am UTC
If you read the file until the fail flag "failbit" gets set then that would be the problem.
Calling open doesn't automatically unset the failbit, you need to do that manually if you want to reuse the same ifstream object again.
A call to clear between closing the old file and opening a new one will do the job fine:
http://www.cplusplus.com/reference/iostream/ios/clear/
Topic archived. No new replies allowed.