Problem with reading files.

Hello!
This function in my program was supposed to read from a group of files of your choosing. The thought is that if you write "X", the function will read the file "X" located under the "Lagring" folder, to find that in file X there are three names of other files that needs reading. These files contain data on Names, Surnames and Grades.
The problem is, that in the code
1
2
3
4
5
while( !FilEtt.eof())
    {
        getline(FilEtt,Betyg[i]);
        i++;
    }


Betyg[i] doesn't seem to contain.. anything? Even though there IS data in the file. This might be confusing, I don't know, but I can probably answer questions better then I can explain here. Please, is there any syntax errors or have I missed something vital?
Will link the entire program if nothing else works.
Thanks in advance!
-Barth

(Code Incoming!)




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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{
    string LesFiler, line[99];
    int i=0;
    system("cls");
    cout<<"\n Vilken filgrupp vill du l\x84sa av?\n ";
    cin>>LesFiler;
    ifstream myfile;
    myfile.open(string("Lagring\\"+LesFiler+".txt").c_str());
    while( !myfile.eof() )
    {
        getline(myfile,line[i]);
        i++;
        cout<<"Hej";
     system("pause");
    }
    myfile.close();

    cout << "IT BEGINS!";
    system("pause");
    i=0;
    ifstream FilEtt;
    FilEtt.open( line[0].c_str());
    while( !FilEtt.eof())
    {
        getline(FilEtt,Betyg[i]);
        i++;
    }
    FilEtt.close();

    cout << "IT CONTINUES!";
    system("pause");
    i=0;
    ifstream FilTwo;
    FilTwo.open( line[1].c_str());
    while( !FilTwo.eof() )
    {
        getline(FilTwo,Namn1[i]);
        i++;
    }
    FilTwo.close();

    cout << "IT'S LAST!";
    system("pause");
    i=0;
    ifstream FilTre;
    FilTre.open( line[2].c_str());
    while( !FilTre.eof() )
    {
    
        getline(FilTre,Namn2[i]);
        i++;
    }
    FilTre.close();

    cout << "IT ENDS!";
    system("pause");



}
Isn't the other files also located in the Lagring directory? Is it mentioned in the file? If isn't you have to add it to the path like you do when opening the first file.

You could also check if the files are opened correctly and print a message if they aren't.

Where is Betyg, Namn1 and Namn2 defined. How many elements contain. Why do you think they doesn't contain anything?

It probably doesn't make much difference in this case but you should know that your loops will make one more iteration than necessary. http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
{
    if(Antal!=0)
    {

    ofstream Betygs;
    string FileNameInput, FileNameOutput;

    system("cls");
    cout << "\n Vad vill du kalla den h\x84r filgruppen?";
    getline(cin, FileNameInput);
    getline(cin, FileNameInput);

    FileNameOutput ="Lagring\\";
    FileNameOutput+=FileNameInput;
    FileNameOutput+="-Betyg.txt";
    Betygs.open(string(FileNameOutput).c_str());

    if(Antal!=0)
    {
        if(Betygs.is_open())
        {
            for(int i = 1; i <= Antal; i++)
            {
                Betygs<<Betyg[i]<<endl;
            }
            Betygs.close();
        }
    if( remove( ".txt" ) != 0 )
    perror( "" );
    puts( "" );

    FileNameOutput ="Lagring\\";
    FileNameOutput+=FileNameInput;
    FileNameOutput+="-Fornamn.txt";
    ofstream Namn;
    Namn.open(string(FileNameOutput).c_str());


        if(Namn.is_open())
        {
            for(int i = 1; i <= Antal; i++)
            {
                Namn<<Namn1[i]<<endl;
            }
            Namn.close();
        }
    FileNameOutput ="Lagring\\";
    FileNameOutput+=FileNameInput;
    FileNameOutput+="-Efternamn.txt";
    ofstream Efternamn;
    system("cls");
    Efternamn.open(string(FileNameOutput).c_str());


        if(Efternamn.is_open())
        {
            for(int i = 1; i <= Antal; i++)
            {
                Efternamn<<Namn2[i]<<endl;
            }
            Efternamn.close();
        }
    FileNameOutput ="Lagring\\";
    FileNameOutput+=FileNameInput;
    FileNameOutput+=".txt";
    ofstream LagringsEnhet;
    system("cls");
    LagringsEnhet.open(string("Lagring\\"+FileNameInput+".txt").c_str());


        if(LagringsEnhet.is_open())
            {


                LagringsEnhet<<"Lagring\\"+FileNameInput+"-Betyg.txt\n";
                LagringsEnhet<<"Lagring\\"+FileNameInput+"-Fornamn.txt\n";
                LagringsEnhet<<"Lagring\\"+FileNameInput+"-Efternamn.txt\n";

            }
            LagringsEnhet.close();
        }
    }
}


There's how it creates the files from the start, as you can see they end up in the same directory. This is also mentioned in the file, but should there be double backslashes in the file? I've tried with both Single and Double backslashes but with no difference.
Namn1, Namn2 and Betyg are defined as Namn1[99], Namn2[99] and Betyg[99] in the beginning of the class.
The loop adding one more iteration than necessary SHOULDN'T be a problem as far as I know. But I'm a newbie, I'm not sure.
I've checked if the file is opened correctly, but when I check Betyg, like this:
1
2
3
4
5
6
7
8
9
10
 while( !FilEtt.eof())
    {
        getline(FilEtt,Betyg[i]);
        i++;
        cout<<Betyg[i] (NOTHING SHOWS UP WHEN I EXECUTE THIS!)
        cout<<Betyg[0] (Shows up the first and second time it loops.)
        cout<<Betyg[1] (Shows up the second time it loops.)


    }


I find this very strange. Any ideas what might be the issue/s?

Have I messed up somewhere just so that my Printing function won't work?
I mean.. It's just a simple for loop.
1
2
3
4
5
6
    for(int N=1 ; N!=Antal+1 ; N++ )
    {

        cout<<"       "<<Namn1[N]<<" "<<Namn2[N]<<" Med betyget : "<<Betyg[N]<<endl;

    }
Last edited on
cout<<Betyg[i] (NOTHING SHOWS UP WHEN I EXECUTE THIS!)
That is because you haven't written anything to Betyg[i] yet. Put this line above i++; instead of below it.
Oh. God I feel silly, but well.. The true problem is that when I then try to print it using my print function.. I get the first line right, then like 50 empty lines, then a bugged line, then the program crashes. And I don't know why..
Anyone know why this is?
Are you sure that Antal < 98? I say 98 here because you start indexing at 1 instead of 0 so you never use the first element in the array.
I actually think I see the problem now. It's looking for Antal, which doesn't get a value since when I manually input something into the program it gets a value the first thing it does, since my first input is "How many people would you like to input?". This might sounds confusing.. Will look into it.
Topic archived. No new replies allowed.