I have a text document namelist.txt
This file contains a list of names in this format:
Aimee
Alice
Alicia
Allison
etc.
Here's what I want to do.
-Open the file for reading
-Read the first line
-Assign the first line "Aimee" to variable[0]
-Read the second line
-Assign the second line "Alice" to variable[1]
-Continue until EOF.
Here's what I've written so far.
The problem is the "++i" line. It assigns the value to names[0] just fine, but then on the next loop it crashes. I don't understand why this is happening. Maybe I'm going about this the wrong way. Any help would be appreciated.Code:#include <iostream> #include <fstream> using namespace std; int main() { ifstream fin; char c[11]; char names[50][11]; int i=0; fin.open("namelist.txt", ios::in); if(fin.fail()) { cout << "Error: Unable to open namelist.txt\n"; exit(1); } while(!fin.fail() && !fin.eof()) { // cout << c <<endl; fin.getline(c,11,'\n')>>names[i]; // strcpy(names[i],c); ++i; } fin.close(); system("PAUSE"); return(0); }