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

CString and wsprintf

70 views
Skip to first unread message

Herbert Enderton

unread,
Jan 28, 1997, 3:00:00 AM1/28/97
to

Something I learned the hard way today. Don't do this:

CString sFoo, sBar, sCombo;
sFoo = "answer";
sBar = "is";
int n = 42;
wsprintf (sCombo.GetBuffer(99), "The %s %s %d.",
sFoo, sBar, n); // bad
sCombo.ReleaseBuffer();

The compiler doesn't know enough about the types of wsprintf's arguments
to cast the CString into a LPCSTR or whatever. With MSVC 1.52 and
the large memory model, the above code compiled fine but produced
an sCombo of "The answer 3762". But with MSVC 4.0, code similar to
this worked okay.

To get around this problem, the wsprintf line should perhaps read

wsprintf (sCombo.GetBuffer(99), "The %s %s %d.",
(const char *) sFoo, (const char *) sBar, n); // better

This works, and I think sBar.GetBuffer(0) would work instead of the
cast. But my understanding of this whole situation is kind of shaky.

The code is still not so good because if the wsprintf tries to put more than
99 characters into sCombo, I don't know what will happen. Besides, it's ugly.
What is a more idiomatic C++/MFC way to write that formatted print statement?

-- Bert Enderton


Sam Blackburn

unread,
Jan 28, 1997, 3:00:00 AM1/28/97
to

Why not use
sCombo.Format( "The %s %s %d.", (LPCTSTR) sFoo, (LPCTSTR) sBar, n );

Herbert Enderton <h...@cs.cmu.edu> wrote in article
<5cjs7u$g...@cantaloupe.srv.cs.cmu.edu>...

pazi

unread,
Jan 28, 1997, 3:00:00 AM1/28/97
to

h...@cs.cmu.edu wrote:
>What is a more idiomatic C++/MFC way to write that formatted print statement?

Check out CString::Format()

>With MSVC 1.52 and...

However, it's not documented for 1.52 (but is there), so you either
need to inspect the sources (I suppose strcore.cpp) or look up the
docs in VC4 or VC2.

Since the CString::Format() is pretty much just grabbed from
the 32-bit MFC into 1.52 (MFC2.52), IT DOESN'T FORMAT LONGS CORRECTLY!
=================================
If you want to format longs, use wsprintf instead, or patch the
MFC sources (the right point is where the formatter sees a long,
thinks it doesn't affect size and only pops an int out of the va_args).

-zi
--
(Please note that the From: address is invalid. To mail me,
remove the ".---", or use the Reply-to: address.)
pa...@to.icl.fi


Vadim Berman

unread,
Jan 29, 1997, 3:00:00 AM1/29/97
to

Herbert Enderton wrote:
>
> Something I learned the hard way today. Don't do this:
>
> CString sFoo, sBar, sCombo;
> sFoo = "answer";
> sBar = "is";
> int n = 42;
> wsprintf (sCombo.GetBuffer(99), "The %s %s %d.",
> sFoo, sBar, n); // bad
> sCombo.ReleaseBuffer();
>
> The compiler doesn't know enough about the types of wsprintf's arguments
> to cast the CString into a LPCSTR or whatever. With MSVC 1.52 and
> the large memory model, the above code compiled fine but produced
> an sCombo of "The answer 3762". But with MSVC 4.0, code similar to
> this worked okay.
>
> To get around this problem, the wsprintf line should perhaps read
>
> wsprintf (sCombo.GetBuffer(99), "The %s %s %d.",
> (const char *) sFoo, (const char *) sBar, n); // better
>
> This works, and I think sBar.GetBuffer(0) would work instead of the
> cast. But my understanding of this whole situation is kind of shaky.
>
> The code is still not so good because if the wsprintf tries to put more than
> 99 characters into sCombo, I don't know what will happen. Besides, it's ugly.

Apparently you'll corrupt your heap.

> What is a more idiomatic C++/MFC way to write that formatted print statement?


sCombo.FormatMessage("The %1 %2 %3!d!.",
(LPCTSTR)sFoo, (LPCTSTR)sBar, n);


Harald Egger

unread,
Jan 29, 1997, 3:00:00 AM1/29/97
to Herbert Enderton

Herbert Enderton wrote:
>
> To get around this problem, the wsprintf line should perhaps read
>
> wsprintf (sCombo.GetBuffer(99), "The %s %s %d.",
> (const char *) sFoo, (const char *) sBar, n); // better
>
> This works, and I think sBar.GetBuffer(0) would work instead of the
> cast. But my understanding of this whole situation is kind of shaky.
>
> The code is still not so good because if the wsprintf tries to put more than
> 99 characters into sCombo, I don't know what will happen. Besides, it's ugly.
> What is a more idiomatic C++/MFC way to write that formatted print statement?
>
> -- Bert Enderton

Hi!

I think you are looking for this:

sCombo.Format ("The %s %s %d.", sFoo, sBar, n);

I hope, that this will help you.
Good luck!

Harald Egger

William E. Kempf

unread,
Jan 30, 1997, 3:00:00 AM1/30/97
to

Vadim Berman <b...@digideas.com.au> wrote:

:Herbert Enderton wrote:
:>
:> Something I learned the hard way today. Don't do this:
:>
:> CString sFoo, sBar, sCombo;
:> sFoo = "answer";
:> sBar = "is";
:> int n = 42;

:> wsprintf (sCombo.GetBuffer(99), "The %s %s %d.",
:> sFoo, sBar, n); // bad


:> sCombo.ReleaseBuffer();
:>
:> The compiler doesn't know enough about the types of wsprintf's arguments
:> to cast the CString into a LPCSTR or whatever. With MSVC 1.52 and
:> the large memory model, the above code compiled fine but produced
:> an sCombo of "The answer 3762". But with MSVC 4.0, code similar to
:> this worked okay.

:>
:> To get around this problem, the wsprintf line should perhaps read


:>
:> wsprintf (sCombo.GetBuffer(99), "The %s %s %d.",
:> (const char *) sFoo, (const char *) sBar, n); // better
:>
:> This works, and I think sBar.GetBuffer(0) would work instead of the
:> cast. But my understanding of this whole situation is kind of shaky.
:>
:> The code is still not so good because if the wsprintf tries to put more than
:> 99 characters into sCombo, I don't know what will happen. Besides, it's ugly.

:
:Apparently you'll corrupt your heap.
:
:> What is a more idiomatic C++/MFC way to write that formatted print statement?
:
:
:sCombo.FormatMessage("The %1 %2 %3!d!.",
: (LPCTSTR)sFoo, (LPCTSTR)sBar, n);
:

Or sCombo.Format("The %s %s %d.", (LPCTSTR)sFoo, (LPCTSTR) sBar, n);

Note, you still need to cast to LPCTSTR (prefered over const char* to
stay UNICODE compliant).

-----
William E. Kempf : http://www.novia.net/~srwillrd
"Sir Willard" : mailto:srwi...@novia.net (home)
Knight of the Ascii Table :

David R. Connet

unread,
Feb 6, 1997, 3:00:00 AM2/6/97
to

In article <32EF3E...@siemens.at>, harald...@siemens.at wrote:

>Herbert Enderton wrote:
>>
>> To get around this problem, the wsprintf line should perhaps read
>>
>> wsprintf (sCombo.GetBuffer(99), "The %s %s %d.",
>> (const char *) sFoo, (const char *) sBar, n); // better
>>
>> This works, and I think sBar.GetBuffer(0) would work instead of the
>> cast. But my understanding of this whole situation is kind of shaky.
>>
>> The code is still not so good because if the wsprintf tries to put more than
>> 99 characters into sCombo, I don't know what will happen. Besides, it's
> ugly.
>> What is a more idiomatic C++/MFC way to write that formatted print statement?
>>
>> -- Bert Enderton
>
>Hi!
>
>I think you are looking for this:
>
> sCombo.Format ("The %s %s %d.", sFoo, sBar, n);
>
>I hope, that this will help you.
>Good luck!
>
>Harald Egger

But you still need to cast the sFoo and sBar variables. CString::Format is no
smarter than wsprintf in this matter.
sCombo.Format ("The %s %s %d.", (LPCTSTR)sFoo, (LPCTSTR)sBar, n);


--------------------
Dave Connet

email address is intentionly wrong - you know why! (there's no '1')

0 new messages