Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Concat'ing an AnsiString

1,141 views
Skip to first unread message

Michael Lerch

unread,
Jun 25, 1999, 3:00:00 AM6/25/99
to
Hi, i'm rather new at C++, been using delphi for a long time tho.
I've tried to concat an ansistring like this:
AnsiString S = "blah" + "blah";
and it just doesnt seem to work (works in delphi)

i looked at the help file and it says:
Concatenates two AnsiStrings.
friend AsiString __fastcall operator +(const char* lhs, const AnsiString&
rhs);
Returns an AnsiString that is the concatenation of strings lhs and rhs.
AnsiString __fastcall operator +(const AnsiString& rhs) const;
Returns an AnsiString that is the concatenation of this AnsiString and rhs.

Can someone help me interpret how to do this? (ie just give me and example
how to do this)
it sounds silly, but i cant seem to figure it out =<
Thanks,
Michael

Liz Albin

unread,
Jun 25, 1999, 3:00:00 AM6/25/99
to
Try this:

AnsiString aString = AnsiString("This ") + " and that";


Liz Albin

unread,
Jun 25, 1999, 3:00:00 AM6/25/99
to
Try this

AnsiString aString = AnsiString(" This ") + " and that";

(The conversion of char * to AnsiString is the problem)


Vijay

unread,
Jun 25, 1999, 3:00:00 AM6/25/99
to
Hi, while on the subject. I want to concatenate a string, an integer and a
string, and assign it a string. Say, something like this:

AnsiString File1 = "text";
int i ;
for (i=0;i<10;i++)
File1 += IntToStr(i) + ".trf";

Presently, at the end of it, I still have text in File1, instead of
text0.trf, etc. Any suggestions on what I am doing wrong, or how to add an
integer to a string?

thanks,
--

Vijay
______________________
Vijay Gopal Kovvali
301 Ball Street #2087
College Station, TX 77840
Home: (409) 862-9288
Office: (409) 845-9892
Liz Albin <Elizabe...@bowne.com> wrote in message
news:7l089e$fg...@forums.borland.com...

Vik

unread,
Jun 25, 1999, 3:00:00 AM6/25/99
to

Vijay wrote:

Hi,
i don't understand what you mean, the snippet runs as it should, assigning
File1=="text0.trf1.trf2.trf ecc. ecc. ecc.".

Regards

Vittorio Digilio


Vijay

unread,
Jun 25, 1999, 3:00:00 AM6/25/99
to
It doesn't work on my machine. I tried the same code on one of my coworkers
machine, now, and it works fine, the way it should. On mine it just stores
the wrong value (it doesn't add the integer and the rest of the part), no
errors of any sort. I spent a whole day on it, and still can't see why....

Thanks for looking at it.

Vijay

Vik <stra...@server.inmedia.it> wrote in message
news:3773ED2F...@server.inmedia.it...

Remy Lebeau

unread,
Jun 25, 1999, 3:00:00 AM6/25/99
to
Based on the code you supplied, try this:

AnsiString File1 = "text";
int i ;
for (i=0;i<10;i++)

File1 += AnsiString(IntToStr(i) + ".trf");

This will produce a string like this:
"text0.trf1.trf3.trf"...

Is that what you wanted the string to be like? Or did you want strings like
"text0.trf", "text1.trf", etc..? If the latter, then your originally-posted
code is wrong for this.


Gambit

Vijay <vgk...@unix.tamu.edu> wrote in message
news:7l0ibu$ft...@forums.borland.com...

Vik

unread,
Jun 26, 1999, 3:00:00 AM6/26/99
to
Hi Vijay,

try to build all the project (from the menu), not to make it (CTRL + F9).
Alternatively you may try to re-assemble it from scratch, if it's not too big.
Maybe it helps.
Otherwise you should post the whole snippet of code where the for(...) it is.

Regards,

Vittorio Digilio


Vijay

unread,
Jun 26, 1999, 3:00:00 AM6/26/99
to
I want text0.trf, text1.trf, etc. and my actual code does that. It is about
three files long, so I just described the place I was having the problem
thinking that I was doing something wrong (in concatenating strings). As I
wrote earlier, I am not getting the output that all of you are getting, for
no apparent reason. My actual function, I attached below.

I have one edit box, one CSpinEdit and a checkbox. The edit box takes the
name of a file, the CSpinEdit takes an integer, and checkbox tells whether I
should create a CORSIM file (some software file we use). If the checkbox is
checked, I read the editbox file. This editbox file contains a list of
files. My function reads each filename contained in the editbox file, and
creates copies of this file (as many as the number given in CSpinEdit) by
adding 0,1,2, etc. Then all these copies are written to another file (.inp
file).

The place I am having problem is
TempFileName2 = TempFileName2 + IntToStr(i) + ".trf";
If TempFileName2 has "text" earlier, after this I should have "text0.trf",
etc., instead, I am getting "text". Using the debugger I can see that
TempFileName2 contains the correct text before this operation. Seeing from
your responses and my friends here, I see that there is nothing wrong with
this line of code.

Vijay

// Please note that I have copied the form variables into my own user
variables

void TRandRun::Run()
{
int i;
char filebuff[81];
AnsiString TempFileName1;
AnsiString TempFileName2;
fstream CORFile;

fstream SimFile(SimFileName.c_str());
if(CreateListFile) // if the checkbox is checked
{
TempFileName1 = SimFileName; // SimFileName is the name in the editbox
i = TempFileName1.Pos(".");
TempFileName1.Delete(i+1,3);
CORListFileName = TempFileName1 + "inp";
CORFile.open(CORListFileName.c_str());
CreateListFile = false; file://So that the loop doesn't execute again
} // All this to create the inp file for CORSIM Input File

SimFile.getline(filebuff, sizeof(filebuff));
istrstream ReadInto(filebuff,strlen(filebuff));
// ReadInto is the stringstream
ReadInto>>TempFileName1.c_str();
TempFileName2 = TempFileName1;
i = TempFileName2.Pos(".");
TempFileName2.Delete(i,4);

for(i=0;i<NoOfSimFiles;i++){
// sprintf(TempFileName1.c_str(), "%s%d.trf", TempFileName2.c_str(),i);
// I tried sprintf also without success
TempFileName2 = TempFileName2 + IntToStr(i) + ".trf";
CopyFile(TempFileName1.c_str(), TempFileName2.c_str(),false);
CORFile<<TempFileName2.c_str()<<endl;

Remy Lebeau

unread,
Jun 26, 1999, 3:00:00 AM6/26/99
to
Try this:

TempFileName2 = AnsiString(TempFileName2 + IntToStr(i) +
AnsiString(".trf"));


Gambit

Vijay <vgk...@unix.tamu.edu> wrote in message

news:7l3gdg$ik...@forums.borland.com...

0 new messages