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

put an integer at the end of string

P: n/a
Hello everybody,

I've got a problem. I want to concatenate a string with an integer
value. How can I do that?

for example.

int number = 1;
char *string;
string = "file";

How do I get to string = "file1" or string = "1file" ??
Do I need an explicit cast or is there any trick?

I need that to enumerate a sequenze of filenames.

Thank u in advance for your afforts.

Bernd
Nov 13 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
You may use the function named "sprintf" .
U had better use the function named "snprintf"

snprintf is more security than sprintf

"Bernd Schuster" <be*****@gmx.de>
??????:54*************************@posting.google. com...
Hello everybody,

I've got a problem. I want to concatenate a string with an integer
value. How can I do that?

for example.

int number = 1;
char *string;
string = "file";

How do I get to string = "file1" or string = "1file" ??
Do I need an explicit cast or is there any trick?

I need that to enumerate a sequenze of filenames.

Thank u in advance for your afforts.

Bernd

Nov 13 '05 #2

P: n/a

hercules <we*****@harbournetworks.com> wrote in message
news:bd***********@mail.cn99.com...
You may use the function named "sprintf" .
U had better use the function named "snprintf"

snprintf is more security than sprintf


And of course be mindful of the fact that unless you have a C99 compiler,
snprintf is non-standard. For example, you have to use _snprintf on MSVC.
In cases like that, you might want to do what I did, which is to have a
header that (among other things) #defines snprintf _snprintf.

Alternatively (and much more involved, so I haven't done it) is to rename
the headers in your compiler's include directory. Then, write a header with
the old name which includes the renamed header, followed by the C99
enhancements. Actually, it would be nice if somebody wrote a patch that did
that to MSVC's include directory to make it C99 compliant wherever possible.

Of course, many of the changes, like the snprintf problem, inttypes.h, and
iso646.h, are trivial. Others, such as complex.h are difficult or
impossible; barring hacks to the compiler itself.

--$teve

Nov 13 '05 #3

P: n/a
Bernd Schuster wrote:

Hello everybody,

I've got a problem. I want to concatenate a string with an integer
value. How can I do that?

for example.

int number = 1;
char *string;
string = "file";

How do I get to string = "file1" or string = "1file" ??
Do I need an explicit cast or is there any trick?

I need that to enumerate a sequenze of filenames.

Thank u in advance for your afforts.


/* BEGIN fileXX.c */

#include <stdio.h>

int main (void)
{
char array[] = "fileXX";
size_t tens, ones;

for (tens = 0; tens != 10; ++tens) {
for (ones = 0; ones != 10; ++ones) {
array[4] = (char)(tens + '0');
array[5] = (char)(ones + '0');
puts(array);
}
}
return 0;
}

/* END fileXX.c */
--
pete
Nov 13 '05 #4

P: n/a
Thank u all, solved it with your hints.

Bernd
Nov 13 '05 #5

P: n/a
be*****@gmx.de (Bernd Schuster) wrote in message news:<54*************************@posting.google.c om>...
Hello everybody,

I've got a problem. I want to concatenate a string with an integer
value. How can I do that?

for example.

int number = 1;
char *string;
string = "file";

How do I get to string = "file1" or string = "1file" ??
Do I need an explicit cast or is there any trick?

I need that to enumerate a sequenze of filenames.

Thank u in advance for your afforts.

Bernd


Why not just strcat(string, itoa(number)) ?
Nov 13 '05 #6

P: n/a
cris wrote:
be*****@gmx.de (Bernd Schuster) wrote in message
news:<54*************************@posting.google.c om>...
Hello everybody,

I've got a problem. I want to concatenate a string with an integer
value. How can I do that?

for example.

int number = 1;
char *string;
string = "file";

How do I get to string = "file1" or string = "1file" ??
Do I need an explicit cast or is there any trick?
Why not just strcat(string, itoa(number)) ?


Because that would be seriously wrong in the context above.

(`string` doesn't point to enough storage, and that storage isn't
[definedly] writeable anyway. Either enough store should be malloced,
or `string` should be `[numberBigEnough]`ed, not `*`ed.)

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #7

P: n/a
Hi,

You can use sprintf function to achieve that.

for eg;

sprintf (string, "%s%d", string, number);

sumit

be*****@gmx.de (Bernd Schuster) wrote in message news:<54*************************@posting.google.c om>...
Hello everybody,

I've got a problem. I want to concatenate a string with an integer
value. How can I do that?

for example.

int number = 1;
char *string;
string = "file";

How do I get to string = "file1" or string = "1file" ??
Do I need an explicit cast or is there any trick?

I need that to enumerate a sequenze of filenames.

Thank u in advance for your afforts.

Bernd

Nov 13 '05 #8

P: n/a

<su**********@wipro.com> wrote in message
news:94**************************@posting.google.c om...
You can use sprintf function to achieve that. sprintf (string, "%s%d", string, number);


I HATE having sprintf() write over one of its arguments. I don't believe
(any comments?) that it is legal, either, though it often seems to work.

How about:

sprintf(string+strlen(string),"%d",number);

It is, of course, your responsibility to make sure that string is long
enough to hold the addition.

Consider, for example, sprintf(string,"%f",cray_number) when running on a
Cray with 64 bit floating point format with 16 bits of exponent. How big
should string be?

-- glen
-- glen


Nov 13 '05 #9

P: n/a
"Glen Herrmannsfeldt" <ga*@ugcs.caltech.edu> writes:
<su**********@wipro.com> wrote in message
news:94**************************@posting.google.c om...
You can use sprintf function to achieve that.
sprintf (string, "%s%d", string, number);


I HATE having sprintf() write over one of its arguments. I don't believe
(any comments?) that it is legal, either, though it often seems to work.


It is undefined behavior, see C99 7.19.6.6 "The sprintf
function":

If copying takes place between objects that overlap, the
behavior is undefined.
Consider, for example, sprintf(string,"%f",cray_number) when running on a
Cray with 64 bit floating point format with 16 bits of exponent. How big
should string be?


Difficult to say, so snprintf() would be preferable.
--
"This is a wonderful answer.
It's off-topic, it's incorrect, and it doesn't answer the question."
--Richard Heathfield
Nov 13 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.