.append() issue

I'm trying to load files from a folder and I have a directory name that I push back into the vector then it appends the name of the character to load, and it gets the first one but not the second Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    string charDirectory = "Resources/Characters/";

    ifstream loadCharacters;

    //Get Characters
    if(loadCharacters.is_open())
    {
        while(!loadCharacters.eof())
        {
            while(getline(loadCharacters, gameVars.characterName))
            {
                gameVars.storeCharacters.push_back(charDirectory.append(gameVars.characterName));

                for(int i = 0; i < gameVars.storeCharacters.size(); i++)
                {
                    cout << gameVars.storeCharacters[i] << endl;
                }
            }
        }
        loadCharacters.close();
    }


Here is the output:

Resources/Characters/$Player.png
Resources/Character/$Player.png$Player2.png

$Player2.png is supposed to appear like the first line, but instead its appended to the first so how do i fix this? I have been struggling with it for at least a day.
1
2
3
4
    if(loadCharacters.is_open())
    {
        while(!loadCharacters.eof())
        {
these are not needed when you have while(getline(loadCharacters, gameVars.characterName)) since the getline will return a std::ifstream & which will invoke operator bool when it checks the conditions. The bool will return false if there are any bad bits/fail bits/eof bits set. http://www.cplusplus.com/reference/ios/ios/operator_bool/

As far as the error before line 12 you should set the string back to the default value charDirectory = "Resources/Characters/";. By the way you could also use the +/= operator to append. Something line this:
1
2
3
4
            while(getline(loadCharacters, gameVars.characterName))
            {
                charDirectory = "Resources/Characters/" + gameVars.characterName;
                gameVars.storeCharacters.push_back(charDirectory);
Ok that worked thank you. I hate it when i cant figure out simple stuff :(
Topic archived. No new replies allowed.