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
Herbert Enderton <h...@cs.cmu.edu> wrote in article
<5cjs7u$g...@cantaloupe.srv.cs.cmu.edu>...
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
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);
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
: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 :
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')