can't load string array with getline

I'm trying to load users from a .txt file. I do this in the first lines of main();
I did my self a check to see if the files are being loaded during the loop and they are, but the information isn't getting inside, like if he wouldnt be able to read the file.

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
void LoadAllUserData()
{
     for(int i=0;i<MAX_USERS;i++)
     {
             string fnamestring;
             stringstream convert;
             convert << i;
             fnamestring=convert.str()+".txt";
             if(fexists(fnamestring))
             {
                                  getline(myfile,fname[i],'\n');
                                  cout << "fname[" << i << "]=" << fname[i] << endl;
                                  getline(myfile,lname[i],'\n');
                                  cout << "lname[" << i << "]=" << lname[i] << endl;
                                  getline(myfile,nick[i],'\n');
                                  cout << "nick[" << i << "]=" << nick[i] << endl;
                                  getline(myfile,phone[i],'\n');
                                  cout << "phone[" << i << "]=" << phone[i] << endl;
                                  getline(myfile,mail[i],'\n');
                                  cout << "mail[" << i << "]=" << mail[i] << endl;
                                  cout << fnamestring << " loaded to array slot " << i <<".\n";
                                  Sleep(2000);
             }
     }
}


so, I have only 1 file (0.txt) with my details like:
1
2
3
4
5
Sam
Something
Sam5513
myphonenumb
mymail


When the loop is initiated, it shows me "0.txt loaded to array slot 0.", but the lines above show "fname[0]=" and so on, meaning that there is nothing in the array slots and they are cleared out.
Where is myfile opened?
Chervil is right, if you open a file in main, it's not open for other functions.
Did you check to see if the file was open in your function ?
If not just move the open file statements.
Chervil is right, if you open a file in main, it's not open for other functions.


If you open a file, it's opened until it's closed. Whether the file was opened in main or not has little to do with it.

It would appear the code is checking if the file exists on line 9, which makes one wonder why there is no code after that to open the file.
Topic archived. No new replies allowed.