By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

char + short = char*

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
SinusX wrote:
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
Jul 22 '05 #2

P: n/a

"SinusX" <sinusx[nospam]@wp.pl> wrote in message
news:c8**********@atlantis.news.tpi.pl...
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();
....
}
Jul 22 '05 #3

P: n/a

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:2g************@uni-berlin.de...
stringstream ss;


Actually this could be ostringstream.
Jul 22 '05 #4

P: n/a

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:2g************@uni-berlin.de...

"SinusX" <sinusx[nospam]@wp.pl> wrote in message
news:c8**********@atlantis.news.tpi.pl...
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();


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
Jul 22 '05 #5

P: n/a

"Jeff Flinn" <NO****@nowhere.com> wrote in message
news:c8**********@bluegill.adi.com...

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:2g************@uni-berlin.de...

"SinusX" <sinusx[nospam]@wp.pl> wrote in message
news:c8**********@atlantis.news.tpi.pl...
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.

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.