Help with for loops and strings when loading files.

Started by
7 comments, last by Washu 12 years, 1 month ago
With C++ and SDL I'm having a bit of a brain fart remembering and finding the syntax for what I'm trying to do. I have a for loop in a function that is loading files into an array. (char* file, int number) are the two parameters of the function where file is the first part of the file name and number is the number of files. I have the files named as such:

example0.png
example1.png
example2.png

The code for the for loop is as follows:

for (int i = 0; i < number; i++)
{
Array = OnLoad((file) + i + ".png") ;
}


OnLoad is a function I have for loading files which works independently of this. My question is when I pass the parameters "example" and "3" to the function and thus this for loop how do I have the above translate it into "example0.png" and so on.
Advertisement
You're using C++, so you should be using std::string, but anyways...

std::ostringstream ss;
ss<<file<<i<<".png";
std::string filename = ss.str();

ostringstream is located in the <sstream> header.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.


You're using C++, so you should be using std::string, but anyways...

std::ostringstream ss;
ss<<file<<i<<".png";
std::string filename = ss.str();

ostringstream is located in the <sstream> header.

Is there any reason to do this vs:
OnLoad(string(file) + '0'+i+ ".png")
THe result of '0'+i is an integer. an integer + std::string is not a valid operator combination (there is no appropriate overload).

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Washu's suggestion worked, thanks :) Now to get my rendering function to work. I'll be back if I get stuck again.
You could always cast it. In my opinion that'd still look a lot better than instantiating a whole new class just to concatenate a string
And the instant i is no longer a number between 0 and 9 you would immediately fail. Btw, the character immediately after '9' is ':', which as you can imagine doesn't work out so well in the middle of a filename.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

You could always lexical_cast<> it, which would hide the details of the conversion (as we don't really care about them in this case).
You could always lexical_cast<> it, which would hide the details of the conversion (as we don't really care about them in this case).


True, if he's using boost. If he's using a modern compiler AND C++0x he might have access to std::to_string functionality as well.

However, this is For Beginners, so the best answer is usually the one that requires the fewest number of "If you have X and version of X is greater than Y" or "if you have X installed and configured correctly", which in this case happens to be stringstream.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement