Prompting a user for a valid input file

Hello,

I'm trying to write some code that prompts the user for an input file, checks to see if the file exists, and prompts again if it does not exist. Here is the code that I wrote to do that:

string inputFile;
ifstream myfile;
cout << "Input file name: ";
getline (cin, inputFile);
myfile.open(inputFile.c_str());
while (!myfile.is_open()) {
cout << "Input file name: ";
getline (cin, inputFile);
myfile.open(inputFile.c_str());
}

And then I prompt the user for an output file, and then I have a while loop with the condition ( !myfile.eof() ) where I process the data from the input file and write to the output file.

Here's the thing: If I enter the correct input file name on the first prompt, then my program works just fine. But if I enter a name that doesn't exist on the first prompt, and then enter the right input file, it will exit the input prompting loop and prompt for the output file, but then my program goes into an infinite loop when it gets to the while ( !myfile.eof() ) loop.

Does anyone know why this happens, or what I can do to fix it?
Your loop to open a file should look like this:
1
2
3
4
5
6
7
8
9
10
11
  ifstream infile;

  cout << "Please enter the input file name> " << flush;
  while (true)
    {
    string infilename;
    getline( cin, infilename );
    infile.open( infilename.c_str() );
    if (infile) break;
    cout << "Invalid file. Please enter a valid input file name> " << flush;
    }


Thereafter, don't test on eof()!

Knowing nothing more than what you have told us, I suspect that your infinite loop is because one of your streams is in an error state (that is, not good()) so eof() will never become true.

Always loop against good(), never eof().

Once good() is no longer good, then you can test to see whether the file is eof() or whether some error has occurred.

Hope this helps.
I tried copying and pasting your code, but now there's a different problem. Once again, the program only works correctly if I enter a valid input file on the first prompt. However, if I input an invalid name at first, then it gets stuck in the loop that you posted and keeps on asking me to enter a valid input file name, even after I give it a valid name.
Sounds like your STL could use some improvement.
Try adding the following after line 5:
1
2
    infile.close();
    infile.clear();


If that does not help, then there is something else wrong with your program. Please post the whole code. (Using [code] tags.)
It works now! I actually added those lines to my original code, which still has a loop that tests on eof(), and it works perfectly. So it would appear that testing on eof() no longer causes any problems for me.

Anyway, thank you very much for your help!
*Sigh*

Pay attention:

NEVER loop on eof()!!!!!!

Quoth me:
Once good() is no longer good, then you can test to see whether the file is eof() or whether some error has occurred.


Also, it is rather disingenuous of you to tell me that my code failed when you had modified it to do something I warned you not to do.
Did you not notice the part where I said that my program works perfectly now? Why shouldn't I test on eof() if I did it and I got it to work? Furthermore, the reason I'm doing that is because I found an example from this site where they test on eof().

http://www.cplusplus.com/doc/tutorial/files/

Perhaps you are making an incorrect assumption about my program or how eof() works.

Also, I did not modify your code at all. I copied and pasted it into my main function, and then I commented out everything else. Without those two additional lines from your second post, if I do not enter a valid input file at the first prompt, then the program will get stuck in that loop and repeatedly ask for a valid input file name, even after I enter one. There was nothing disingenuous about what I said.
I cannot give advice to those who won't listen. I'm glad you got your program working.
Alright, fine. I changed my code to loop on good() instead of eof(), and it still works. As far as I can tell, it didn't make any difference. Now will you please explain why I should never loop on eof(), despite the fact that I did it and got my program to work?
Because if an error occurs on input the stream will be set to an error state and eof() will never become true.

When you loop against good(), your loop will terminate no matter what. Once it does, you can check to see if it is eof() or not.

One thing about programming that you must remember: just because it works for you does not make it good practice (safe, portable, correct, etc). You must be careful to learn how to use the available constructs as much as what they are. Many, many extant software bugs and newbie problems (even in industrial systems) are due to someone doing something that has always "worked for me". It's just part of the learning curve. Alas.

I'm glad you've got it working nicely.
Topic archived. No new replies allowed.