Sign in to follow this  
chbrules

[C++]Concatenate Issues

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this