Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

how to concatenate an integer with a string

Hi.
Given a string and an integer, how can I output something to a file, of which the path is the concatenation of the string and the integer?
Thanks!
[168 byte] By [WXY595] at [2007-11-11 10:19:26]
# 1 Re: how to concatenate an integer with a string
You mean like you have files wich hav numbers at the end of them? That's pretty easy.
CString somestring = "C\:\\MyGallery\\AnImage";
int someint = 001;
CString extension = ".jpg"; //You don't need this if you dont need the extension
somestring += IntToString(someint);
somestring += extension; //Again, skip this bit if u dont wanna no extension
Radius at 2007-11-11 20:59:07 >
# 2 Re: how to concatenate an integer with a string
CString is not a standard C++ class, so I would stick to a standard C++ solution:
#include <sstream>
#include <iostream>
using namespace std;
int main()
{
stringstream st;
st<<"C\\:\\MyGallery\\AnImage";//insert the string part to the stream
st<<100; //insert the integer and concatenate it to the previous value
string result;
st>>result; //result=C\:\MyGallery\AnImage100
}
Danny at 2007-11-11 21:00:08 >
# 3 Re: how to concatenate an integer with a string
Oh yes, sorry, I forgot there are other c++ Compilers than C++ builder :) (joke)
I forgot to add that my solution is for BCB
Radius at 2007-11-11 21:01:12 >
# 4 Re: how to concatenate an integer with a string
I think CString is part of MFC library (Visual C++) and not BCB (VCL).
Ivan** at 2007-11-11 21:02:13 >
# 5 Re: how to concatenate an integer with a string
yes, you can replace CString wit String and it woill still work, i took tyhat from the bcb help.
oh, you can also do that the old hardcore C way:
char* somestring = "C:\\AnImage";
int someint = 100;
strcat(somestring, (char*)someint);
Radius at 2007-11-11 21:03:12 >
# 6 Re: how to concatenate an integer with a string
Thi scertainly won't work since strcat attempts to append additional bytes at the end of the array somestring. This is a classic buffer overflow.
In addition, (char*)someint does something quite different: it casts the value 100 to a pointer address, 0x0000064 which is an invalid address on most platforms.
Danny at 2007-11-11 21:04:11 >