Public Group

Help with for loops and strings when loading files.

This topic is 2792 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

Share on other sites
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.

Share on other sites

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")

Share on other sites
THe result of '0'+i is an integer. an integer + std::string is not a valid operator combination (there is no appropriate overload).

Share on other sites
Washu's suggestion worked, thanks Now to get my rendering function to work. I'll be back if I get stuck again.

Share on other sites
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

Share on other sites
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.

Share on other sites
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).

Share on other sites
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.

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 30
• 9
• 16
• 12