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

Appending formatted text to a CString

672 views
Skip to first unread message

Jeff

unread,
Apr 29, 1999, 3:00:00 AM4/29/99
to
I want to append strings to an existing CString, where
each "to be appended string" has to be built via sprintf type
functionality. I understand += and Format I think, but I'm not
sure how to combine the two. Do I have to use a temp
Cstring or get access to the CString buffer? Or is there
a slick way along the lines of something like:

CString s;

s="first";
s.Format("%s %s", s, "second");

That doesn't work but you catch my drift


Lance S. Doddridge

unread,
Apr 29, 1999, 3:00:00 AM4/29/99
to
try this - I know a lot of people don't like type casting, but it works.
 
char* pch = " world";
CString txt = "Hello";
 
txt += (CString)(const char*)pch;
 
txt now points to a buffer containing "Hello world"
 
Jeff wrote in message ...

Jeff

unread,
Apr 29, 1999, 3:00:00 AM4/29/99
to
I don't understand. How does that relate to what I'm
trying to find a shortcut for? An example...

CString s="There were ";
char a[50];
int v=4;
int u=2;

sprintf(a, "%d ducks", v);
s+=a;
sprintf(a, "and %d cats", u);
s+=a;

I'm wondering if there is a way to eliminate a and
the sprintf's (ie do it without allocating temp space).
Note that what I'm not showing is I'm building a BIG
CString full of many different run-time calculated/retrieved
values.


Lance S. Doddridge <lsdod...@earthlink.net.nospam> wrote in message
news:7g9oet$jtv$1...@holly.prod.itd.earthlink.net...

Anti_Snoop

unread,
Apr 29, 1999, 3:00:00 AM4/29/99
to
If you have all the values at the time you build the string then just build
it.

CString s;
int v=4;
int u=2;

s.Format(_T("There were %d ducks, and %d cats"), v, u);

- Anti_Snoop
http://www.snoopsoft.com


Jeff <phase_...@hotmail.com> wrote in message
news:O0vKPWlk#GA....@cppssbbsa02.microsoft.com...

Johan Rosengren

unread,
Apr 29, 1999, 3:00:00 AM4/29/99
to
Jeff!

If you are building a big CString, you will get massive reallocations if you
try to shuffle in data in the middle of the string. Have you thought about
using a 'straight' memory block instead, doing more primitive concatenations
with small temporary strings instead?
If you are just adding text to an existing string, as Lance (and I)
presumed, then you will loose little by using a temp for the Format, and the
just add it with +=.

Johan Rosengren
Responsable Informatique
PACTA S.A.

Jeff a écrit dans le message ...

Lance S. Doddridge

unread,
Apr 30, 1999, 3:00:00 AM4/30/99
to
It looked like you were trying to add a char* to a CString.  From your sample code, that's what you are doing, since 'a' points to the first item in the char array.  The example I gave does just that - it adds, or appends, the text in a buffer pointed to by a char* to the end of a CString.  Whether or not you use the sprintf() function to get the string in the buffer is up to you.  CString has all the functionality, and more, that the C string libraries have, so it would probably be nicer to use the CString functions since you already have a CString object.
 
To get the string you want, I would do something like the following:
 
CString s;
int d = number_of_ducks;
int c = number_of_cats;
s.Format("There were %d ducks and %d cats.",d,c);
 
 
Jeff wrote in message ...
>I don't understand.  How does that relate to what I'm
>trying to find a shortcut for?  An example...
>
>CString s="There were ";
>char a[50];
>int v=4;
>int u=2;
>
>sprintf(a, "%d ducks", v);
>s+=a;
>sprintf(a, "and %d cats", u);
>s+=a;
>
>I'm wondering if there is a way to eliminate a and
>the sprintf's (ie do it without allocating temp space).
>Note that what I'm not showing is I'm building a BIG
>CString full of many different run-time calculated/retrieved
>values.
>
>
0 new messages