473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

put an integer at the end of string

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
9 16892
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

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
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
Thank u all, solved it with your hints.

Bernd
Nov 13 '05 #5
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
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
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

<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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: news.hku.hk | last post by:
could you tell me how can i convet a string to integer?? #include <iostream> #include <string> using namespace std; int main(){ int integer; string buffer("123456789");
7
by: Sashi | last post by:
Two questions: (1) I can pull the text of an XML element as a string just fine using code as such: strSomeString = myXmlDoc.SelectSingleNode("/Element1/Element2/Element3",...
2
by: Jim in Arizona | last post by:
I'm learning form an ASP.NET 1.0 book and I tried out some code that returns this error: Compiler Error Message: BC30311: Value of type 'Integer' cannot be converted to...
5
by: Barry | last post by:
Hello, In VS2003, I tried using an enum and setting it into a field in a datarow. It seems as if the datatype of the field in the row determined what went into the field. If the datatype was...
11
by: nephish | last post by:
Hello there, i need a way to check to see if a certain value can be an integer. I have looked at is int(), but what is comming out is a string that may be an integer. i mean, it will be formatted...
6
by: comp.lang.php | last post by:
I'm involved in a rather nasty debate involving a strange issue (whereby the exasperated tell me to RTFM even after my having done so), where this is insanely possible: print_r(is_int('1'));...
3
by: veeman | last post by:
Can someone please write an example containing one element which value is integer type, and one attribute which value is also integer type: Is it something like this: <SomeAttribute...
1
by: redpayne | last post by:
Okay, I finally got this program to run according to what the book had us build it as. Now prof wants case 2 and case 3 to prompt again for input, check input to see if it is the correct type, then...
1
by: =?Utf-8?B?RG9u?= | last post by:
Hello, I'm creating a web service that will allow people to enter their contact information into a SQL Server table. I get it to work when I enter all of the fields and press the invoke button,...
2
by: robtyketto | last post by:
Greetings, Within my jsp I have HTML code (see below) which accepts input, one of these fields sequence unlike the others is an Integer. <FORM ACTION="wk465682AddFAQ.jsp" METHOD="POST"> Id:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.