Reading from a file goes wrong.

Hello. I started making something for my class and the thing im getting stuck with is this function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void ucitajOdgovore(string asocijacija[21]){
    int brojac=0;
    ifstream fajl;
    string putanja;
    /*srand(time(NULL));    int random=(rand() %5) +1; 
    switch(random){
        case 1: putanja = "Pitanja/1.txt"; break; // putanja do 1.
        case 2: putanja = "Pitanja/2.txt"; break; // putanja do 2.
        case 3: putanja = "Pitanja/3.txt"; break; // putanja do 3.
        case 4: putanja = "Pitanja/4.txt"; break; // putanja do 4.
        case 5: putanja = "Pitanja/5.txt"; break; // putanja do 5.
        default: cout << "Putanja nije pronadjena" << endl;
    }*/
    putanja = "Pitanja/1.txt"; // cause of testing   
    fajl.open(putanja.c_str());
    while(fajl.eof())
    {
        getline(fajl,asocijacija[brojac]);
        brojac++;
    }
    fajl.close();
}


In the main, i pass real string array "asocijacije" in function which i use in it, and when i use it after this fun. i get an empty array. Its like it didnt happend and i cant see where i went wrong. Any opinion would be great, thanks
You don't check if the file was successfully opened. Perhaps you should begin there. You should also check for reaching eof (if, indeed, you should check for eof at all) after an input operation, not before.
What could be the reason for a file not being opened? if its
Topic archived. No new replies allowed.