put an integer at the end of string

B

Bernd Schuster

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
 
H

hercules

You may use the function named "sprintf" .
U had better use the function named "snprintf"

snprintf is more security than sprintf
 
P

pete

Bernd said:
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 */
 
I

istartedi

hercules said:
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
 
C

cris

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)) ?
 
C

Chris Dollin

cris said:
(e-mail address removed) (Bernd Schuster) wrote in message
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.)
 
S

sumit.sharma

Hi,

You can use sprintf function to achieve that.

for eg;

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

sumit
 
G

Glen Herrmannsfeldt

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
 
B

Ben Pfaff

Glen Herrmannsfeldt said:
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.[/QUOTE]

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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top