Hi,
I'm creating a program that needs to read in a filename from stdin, open it, and do some processing, then repeat the process until the user enters "quit." I get the first file processed fine, but it crashes when I try to open the second file. Actually, the program enter the if (! inFile) section of code even if the second filename is correct and the file exists. I think it's because the filestream is bad at the end of the first file. There must be some way to reuse the same file handle. Here is what I've got:
Any idea what I've got wrong? Thanks!Code:int main() { ifstream inFile; string inFilename; Maze m1; cout << "Enter filename with stored maze or \"quit\" to end: "; cin >> inFilename; inFile.open(inFilename.c_str()); while(inFilename != "quit") { // make sure input file was opened properly if (! inFile) { cout << "Error opening file \"" << inFilename.c_str() << "\"\nProgram will now exit.\n"; return 1; } m1.readMaze(inFile); m1.printMaze(); inFile.close(); cout << "Enter filename with stored maze or \"quit\" to end: "; cin >> inFilename; inFile.open(inFilename.c_str()); } return 0; }