This process is not hard since the default libraries included a function to do this. It's called itoa().
Anditoa is nonstandard. A standard function is sprintf.
#include <stdio.h>
int main(void)
{
const char base[] = "filename";
char filename [ FILENAME_MAX ];
int number = 42;
sprintf(filename, "%s%d", base, number);
printf("filename = \"%s\"\n", filename);
return 0;
}
/* my output
filename = "filename42"
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>Though remember sprintf() does not calculate for buffer overflow.
Neither does itoa. ;)
>A safer version of sprintf() is snprintf() which unfortunately isn't very standard yet.
It is standard, but the standard just isn't widely implemented yet. As it is, C89 is the dominant C standard, and snprintf is nothing more than an extension on many compilers.
>Writing your own version of itoa() on the contrary is quite simple.
Yes, but doing it correctly is not as trivial as some would have you believe.
>*p1 ^= *p2;
>*p2 ^= *p1;
>*p1 ^= *p2;
It's beyond me why people still use this.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
» It's beyond me why people still use this.
Bitwise XOR Operator ^
a^b: 1 if both bits are different. 3 ^ 5 is 6.
Yes, I think several of us know this swap trick. To others it may be confusing. It might even be confusing to the compiler such that it doesn't optimize as well as the straightforward approach . Other hacks too .
>>I need to figure out how to append an int to a string
>Ah, yes. Though remember sprintf() does not calculate for buffer overflow.
You must be on a platform with some really huge ints :lol: (whose decimal representation size, by the way, can be accounted for at compile time).
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>It wouldn't be hard for someone to modify it and make it safer.
Don't forget that someone would also have to modify it to make it correct as well as safer. :) I was implying previously that your code has a bug.
» It is standard, but the standard just isn't widely implemented yet.
>Standard only in C99.
Yes, that would be the standard that isn't widely implemented yet. Unless you know of any others that is. ;)
>Bitwise XOR Operator ^
>a^b: 1 if both bits are different. 3 ^ 5 is 6.
I'm well aware of what it does and how it does it. What confuses me is why people still think that it's a neat trick. The XOR swap is difficult to get right because of obscurity, and when it is right, it isn't very flexible. On top of that, it may not be as efficient as you think.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Don't new people bother looking at dates? This thread is only 3 years old... :eek:
WaltP
Posting Sage w/ dash of thyme
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
Don't new people bother looking at dates? This thread is only 3 years old... :eek:
Only... ? ;)
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
> char* output; //output string (already filled with something)
> strcat(output, buffer);
It's not like it's a really good answer to a long standing problem.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
Three years later you repeat post #3 . What would we do without your guidance?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314