Infinite loop upon opening file

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
      }
        
   }
      
}


Thanks,

Zach
When getting the filename, try

cin.getline(filename, 26)
It worked perfectly! I guess the period in what I was typing in was messing with the input. Thanks for your help!
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.
Ah, thanks firedraco. Makes sense now.
Topic archived. No new replies allowed.