Sign in to follow this  
darenking

How do I put an int on the end of a C++ string object?

Recommended Posts

Hello! This is probably a simple thing. If I declare a string object in C++ like so: std::string filename="data"; and I have an int: int level=1; how do I put level on the end of filename? In certain forms of BASIC it would be a simple thing like this: filename="data" filename=filename+level I want to construct "data1.dat" "data2.dat" etc Thanks!

Share this post


Link to post
Share on other sites

#include <sstream> // basic_ostringstream
#include <string> // basic_string

//...

std::ostringstream oss;

oss << "data" << 1 << ".dat";

std::string s = oss.str();

//....

Share this post


Link to post
Share on other sites
The easiest way is to use boost::lexical_cast:

#include <boost/lexical_cast.hpp>

std::string filename="data";
filename += lexical_cast<std::string>(1); // convert the int to a string

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid

#include <sstream> // basic_ostringstream
#include <string> // basic_string

//...

std::ostringstream oss;

oss << "data" << 1 << ".dat";

std::string s = oss.str();

//....


Save yourself the debugging time.

Share this post


Link to post
Share on other sites
Quote:
Original post by doho
If you wanna use C strings use:

char buffer[WHATEVERSIZE];
sprintf(buffer, "data%d.dat", number);


If your going down that route prefer C99's snprintf, if your using VC it doesn't support C99 fully so you'll have to use _snprintf, use some macro magic to select between the two if you want.

if you want to know the reason why check option 2, actually its worth reading the entire article totally related to this thread [grin]

Share this post


Link to post
Share on other sites
This suits my needs just fine:

char buffer[WHATEVERSIZE];
sprintf(buffer, "data%d.dat", number);

Still fiddling around with trying to put two old C strings together, eg if I have

char text1[]="hello";
char text2[]=" world";

and I want to put them together to make
"hello world"

What's the simplest way to do that?
Without doing anything messy like convert to string objects?

Can you use <sstream> with old C strings?


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
#define flag_BufferSize 100

void main()
{
/* Declare the memory pointer. */
char *strString1;
char *strString2;
int intNumber=5;

/* Create the memory. */
strString1 = calloc(flag_BufferSize , sizeof(char));
strString2 = calloc(flag_BufferSize , sizeof(char));

/* Place some stuff in each. */
strcpy(strString1, "Hello");
strcpy(strString2, " world.");

/* Combine em. */
strcat(strString1, strString2);

/* Print em. */
printf(strString1);

/* Append a filename(#intNumber) onto strString2. */
sprintf(strString2, "CPP2Abstract%d", intNumber);

/* Reprint em~*/
printf(strString2);

/* Free em */
free(strString1);
free(strString2);
}



Untested, of course~ xD

Share this post


Link to post
Share on other sites
OK, I've cleverly written my method with the help of you lot of course:

void Message::Add(int line, char* text1, char* text2, char* text3)
{
std::string stext1=text1;
std::string stext2=text2;
std::string stext2=text3;

std::ostringstream buffer;
buffer << text1 << text2 << text3;
m_sLine[line]=buffer.str();
}


You send it three C style strings or string literals and it joins them together and stores it in one of many C++ strings.

One problem though. I want you to be able to send it one, two or three strings, rather than having to send it three.

I know I can do it by overloading the function, ie creating three methods:

void Add(int line, char* text);
void Add(int line, char* text1, char* text2);
void Add(int line, char* text1, char* text2, char* text3="");

Works just fine but seems a little messy! Is there a simpler way? Something to do with default arugments mebbe?

Share this post


Link to post
Share on other sites
Quote:
Original post by joebarnslondon
Works just fine but seems a little messy! Is there a simpler way? Something to do with default arugments mebbe?

If you want to do any kind of operations (like add them in this case) on strings you could better use a string class such as std::string.

Share this post


Link to post
Share on other sites
Quote:
Original post by joebarnslondon
OK, I've cleverly written my method with the help of you lot of course:

void Message::Add(int line, char* text1, char* text2, char* text3)
{
std::string stext1=text1;
std::string stext2=text2;
std::string stext2=text3;

std::ostringstream buffer;
buffer << text1 << text2 << text3;
m_sLine[line]=buffer.str();
}


//....

Works just fine but seems a little messy! Is there a simpler way? Something to do with default arugments mebbe?


I don't see the point in any of this as the string streams work with C-style strings directly aswell, if you didn't notice in my previous example i was using string literals which are immutable C-style strings.

Share this post


Link to post
Share on other sites
1) As mentioned, string streams will append the data pointed at by a char* just fine. However...

2) Strings can be added to each other, or to a char* literal, with the string operator+ - so if all portions are textual, there is no real problem.


string("foo") + string("bar"); // ok
string("foo") + "bar"; // ok
"foo" + string("bar"); // ok
"foo" + "bar"; // not ok! Trying to add two pointer values together


3) boost::lexical_cast is probably the most convenient and reliable way of converting data to a string.

Thus our function could just be like:


void Message::Add(int line, const std::string& data) {
m_sLine[line] = data;
}

// Used like:
Message m;
m.Add(0, "foo"); // implicit conversion to std::string, IIRC
m.Add(1, string("foo") + "bar"); // the addition is processed before the
// function call, so the explicit string creation is needed
m.Add(2, string("file") + boost::lexical_cast<std::string>(whichFile) + ".dat");



Nice and simple :)

Share this post


Link to post
Share on other sites
A guy asks how to add strings, do you really think hes going to be able to get boost to work? Id think that stringstream is the easiest most robust choice vs the C routines.

Share this post


Link to post
Share on other sites
Quote:
Original post by EvilCrap
A guy asks how to add strings, do you really think hes going to be able to get boost to work? Id think that stringstream is the easiest most robust choice vs the C routines.


The Boost solution is built upon stringstreams anyway; it just does some extra "never would have thought of that" stuff to handle some special cases, and provides meaningful error handling for bad usage (via the bad_lexical_cast - IIRC - exception). Anyway, skill with the language is nothing to do with skill in getting someone else's thing to compile, and the boost::lexical_cast provides a nice simple interface :)

Of course, a forum search (or perhaps a Google search restricted to this site) will turn up several versions of a "basic hand-rolled lexical_cast".

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