Jump to content
  • Advertisement
Sign in to follow this  
chbrules

[C++]Concatenate Issues

This topic is 4818 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

To keep it short and sweet; I have a string, 'file', in which I want to concatenate some things on to. I have these to add:
".\\CampaignID00000" + i + "-OpenedEmail.txt";
I want to concatenate those into my string, 'file'. i is an integer datatype. When I try to do so, I get errors about not being able to add pointers to the string type. I imagine it's the way the + operator is overloaded in the string datatype. How would I be able to go about concatenating the above line into a single string? Thank you.

Share this post


Link to post
Share on other sites
Advertisement
Try using std::string instead of just string literals. Or you can use a std::stringstream to build the string instead.

Share this post


Link to post
Share on other sites
could try

// 2 ^ 32 = 4294967296
char *targetstring;
targetstring = new char[strlen(".\\CampaignID00000")+strlen("-OpenedEmail.txt")+strlen("4294967296")+1];
sprintf(targetstring,"%s%i%s",".\\CampaignID00000",i,"-OpenedEmail.txt");
//this is overly complex ;) use std::string

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Try using std::string instead of just string literals. Or you can use a std::stringstream to build the string instead.


Wow, that worked! Why is that? What's the difference between using the std namespace and accessing it through the scope resolution op?

Share this post


Link to post
Share on other sites
A string literal doesn't have type string, it has a type of a char array, which doesn't overload operator+() for string concatenation.

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!