Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use CString::Format like printf 2

Status
Not open for further replies.

titanandrews

Programmer
Feb 27, 2003
130
US
Hello,
From reading the docs on the CString::Format() function, I was under the impression that I could use it just like printf. I want to be able to set the width of the field to 10, but this code prints out garbage for CString. The printf() function however works fine. Can someone please tell me what I am doing wrong?

Code:
CString tmp = "Hello";
tmp.Format("%10s",tmp);	
cout << tmp.GetBuffer(tmp.GetLength()) << endl; //Garbage
tmp.ReleaseBuffer();

printf(&quot;%10s&quot;,&quot;Hello&quot;);   //As expected


many thanks,

Barry
 
[lol] try this and if you have any questions afterward i'll try to answer them.
Code:
CString tmp(&quot;hello&quot;), out;
out.Format(&quot;%10s&quot;, (LPCTSTR)tmp);
cout << (LPCTSTR)out << endl; //works huh? why?

-pete
 
MSDN CString::Format description (&quot;From reading the docs on...&quot;):
...
The call will fail if the string object itself is offered as a parameter to Format. For example, the following code:

CString str = &quot;Some Data&quot;;
str.Format(&quot;%s%d&quot;, str, 123); // Attention: str is also used in the parameter list.

will cause unpredictable results.
...
Substitute str=>tmp - WYSIWYG?..

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top