char + short = char*

S

SinusX

Hello
I creating program which is genereting file names. I have to create char* as
a file name.
Name of file looks like:
char + unsigned short . char
(eg. test12.tmp)
How to conect char with short and make char* from sum
Thanks for help.
 
V

Victor Bazarov

SinusX said:
Hello
I creating program which is genereting file names. I have to create char* as
a file name.
Name of file looks like:
char + unsigned short . char
(eg. test12.tmp)
How to conect char with short and make char* from sum


Read about 'sprintf' function. You could combine your strings and the
number by printing them into another string:

char newname[100] = {0};
sprintf(newname, "%s%d.%s", name, number, extension);

Make sure 'newname' has enough room to accommodate your combination.

HTH

Victor
 
S

Sharad Kala

SinusX said:
Hello
I creating program which is genereting file names. I have to create char* as
a file name.
Name of file looks like:
char + unsigned short . char
(eg. test12.tmp)
How to conect char with short and make char* from sum
Thanks for help.

Something like -

int main(){
stringstream ss;
int i = 12;
ss << "temp";
ss << i;
ss << "temp";
string s;
s = ss.str(); // s is temp12temp
const char *s1 = s.c_str();
....
}
 
J

Jeff Flinn

Sharad Kala said:
Something like -

int main(){
stringstream ss;
int i = 12;
ss << "temp";
ss << i;
ss << "temp";
string s;
s = ss.str(); // s is temp12temp
const char *s1 = s.c_str();

This can be unsafe as s1 is not guaranteed to be valid after the statement
terminates. either copy the chars or use as:

SomeFileOperation( s.c_str() );


Jeff F
 
S

Sharad Kala

This can be unsafe as s1 is not guaranteed to be valid after the statement
terminates. either copy the chars or use as:

SomeFileOperation( s.c_str() );

Well my intention was to tell that c_str member function of std::string class
gives a const char*.
The pointer is valid till you call some non-const member of string on s1.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top