tags:

views:

445

answers:

4

i have code like this

string xml_path(conf("CONFIG"));

xml_path+=FILE_NAME;

Where, conf function returns char * and FILE name is const char *

I want to combine it to one line like

xml_path(conf("CONFIG")).append(FILE_NAME)

how do i do it?

any suggestions ??

+2  A: 
const char * f = "foo";
char * b = "bar";

string s = string( f ) + b;

Note that you cannot use append(-0 because neither of the strings invvolved is a std:;string. If you really want to append, it will have to be two-stage process:

string s ( f );
s.append( b );
anon
A: 
string xml_path(conf("CONFIG"));
xml_path += string(FILE_NAME);

should do the trick.

markh44
+3  A: 

Alternatively, if you want to format variable of different type, use a ostringstream.

eg.

std::ostringstream oss; 
int a = 2; 
char *s = "sometext"; 
oss<<s<<a<<endl; 
cout<<oss.str(); // will output "sometext2"
Ben
+1 Very useful! Thanks!
User1
+6  A: 

Question asked for one line:

string xml_path = string(conf("CONFIG")) + string(FILE_NAME);

(I assume xml_path is the name of the variable, and not some sort of call in a library I don't know about).

Andrew Jaffe