0

I've done some searching here but I think I can't articulate what i'm looking for so here's my post:

I've got multiple files with similar names that I want to open and display in my console one after the other. These files are ascii images that when displayed in order create an animation

the file names are:

  • 8ball1.txt
  • 8ball2.txt
  • 8ball3.txt
  • 8ball4.txt

I want to use the 'int' declaration in the for loop to open the next file in the list every time the loop executes

Hopefully the code below makes sense - Can i partially complete a file name using the for loops int declaration? Is there an alternative?

void animate2() {

for (int x = 1; x < 5; x++) {

    ifstream animation("8ballanimation//8ball<<x<<.txt");

    while (!animation.eof())
    {
        string displayFile;
        getline(animation, displayFile);
        cout << displayFile << endl;

    }
    animation.close();
    Sleep(150);
    system("CLS");
}

}

Jake
  • 31
  • 10
  • What in the world are you trying to do with : `"8ballanimation//8ball< – Moshe Gottlieb Nov 16 '15 at 23:14
  • I did, i did. I'm a newb to coding :) – Jake Nov 16 '15 at 23:16
  • In c++, string literals are not parsed for any sort of bindings - that would suggest runtime processing and c++ is a compiled language, not a runtime or interpreter based language. – Moshe Gottlieb Nov 16 '15 at 23:20
  • not related, but I would rather use `SetConsoleCursorPosition` (from ``; see http://stackoverflow.com/a/15770935/1931985) rather than clearing the screen with a `CLS`... this for two reasons: using `system` is slow because you are creating a child process for every frame of your animation, and it creates some flicker because for some time the console is blank before displaying the next frame. If you put the cursor back to top-left, you can overwrite screen contents without clearing (but don't forget to erase remains from previous frames, e.g. by filling with spaces after each line). – Ale Nov 16 '15 at 23:42
  • don't use `while(...eof...)`. It doesn't do what you think it does. [see here](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – M.M Nov 16 '15 at 23:56

2 Answers2

3

"8ballanimation//8ball<<x<<.txt" does not make sense. Also, avoid using .eof(), check completion by the return of getline.

void animate2() {
    for (int x = 1; x < 5; x++) {
        stringstream ss;
        ss << "8ballanimation" << x << ".txt";
        ifstream animation(ss.str());
        string line;
        while (getline(animation, line))
            cout << line << "\n";
        animation.close();
        Sleep(150);
        system("CLS");
    }
}
A.S.H
  • 29,101
  • 5
  • 23
  • 50
  • probably a typo, but you should use a `ostringstream`: a `istringstream` has no `<<` operator because it is supposed to be an input stream – Ale Nov 16 '15 at 23:38
  • @Ale, lol, you are right. changed it for `stringstream`, no head aches ;) – A.S.H Nov 16 '15 at 23:42
2

With C++11 you have std::to_string.

for (int i = 1; i <= 4; i++) {
  std::string path = "8ball" + std::to_string(i) + ".txt";
  std::ifstream animation(path);
  // Do what you want
}
vincentp
  • 1,433
  • 9
  • 12
  • 1
    constructing `ifstream` directly from `std::string` is also a C++11 feature, before that you needed `c_str()` call also. – Ben Voigt Nov 16 '15 at 23:17