Jump to content
  • Advertisement
Sign in to follow this  
Monkey_Missile

Help with for loops and strings when loading files.

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

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!