Hey all, I'm having some problems reading in from a file. I'm attempting to use a file name that the user of the program types in, open it, and then read data from it. The issue I'm having is that when the program attempts to open the file the console just scrolls in an infinite loop. Any help is greatly appreciated. Here's my code:
void enterCreatures()
{
int choice;
fstream file;
char fileName[26];
cout << "What do you want to do?" << endl;
cout << "\t1. Load my creatures from a file." << endl;
cout << "\t2. Enter one creature manually." << endl;
cout << "Choice: ";
cin >> choice;
cout << endl;
if (choice == 1)
{
cout << "What is the name of the file with your list of creatures? (ex: filename.txt)" << endl;
cout << "File name: ";
cin >> fileName[26];
file.open(fileName, ios::in);
if(!file)
{
cout << fileName << " could not be opened." << endl << endl;
}
else
{
cout << "All creatures from " << fileName << "have been added to the program." << endl << endl;
}
}
}
No, that wasn't the problem. The problem was you were reading into the 27th character of your array (which was out of range anyway) instead of the array itself.