Reputation: 31
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:
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");
}
}
Upvotes: 0
Views: 2170
Reputation: 29332
"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");
}
}
Upvotes: 3
Reputation: 1433
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
}
Upvotes: 2