CString.format
能帮我详细解释一下吗?
谢谢~
能帮我详细解释一下吗?
谢谢~
解决方案 »
- 创建MFC时,在应用程序向导里选择windows sockets,现在想去掉这个选项怎么办
- 我在内存中画了一个bmp图,想将图复制到PNG图片上,应该怎样做?望大侠门指点一下!
- 菜鸟请教选择法问题
- 关于可用分和技术分
- 局域网传输速度不合理!该如何调整?
- 紧急求助:fatal error LNK1106: invalid file or disk full: cannot seek to 0x6ead0861
- 我失业了 痛啊 今天大放血!!! :(
- 怎么把richeditctrl做成左边一小部分不能编辑,就想vc的编辑器一样
- 请问#ifdef _DEBUG...
- 为什么没人回答我呢,难道是问题太菜了么????????????????????
- 如何在一个VC的ocx代码中导入另一个ocx?
- 请问如何捕获鼠标在屏幕中的平面坐标 并 实时跟随 鼠标显示该点坐标值
void Format( LPCTSTR lpszFormat, ... );void Format( UINT nFormatID, ... );ParameterslpszFormatA format-control string.nFormatIDThe string resource identifier that contains the format-control string.ResCall this member function to write formatted data to a CString in the same way that sprintf formats data into a C-style character array. This function formats and stores a series of characters and values in the CString. Each optional argument (if any) is converted and output according to the corresponding format specification in lpszFormat or from the string resource identified by nFormatID.The call will fail if the string object itself is offered as a parameter to Format. For example, the following code:CString str = "Some Data";
str.Format("%s%d", str, 123); // Attention: str is also used in the parameter list.will cause unpredictable results.When you pass a character string as an optional argument, you must cast it explicitly as LPCTSTR. The format has the same form and function as the format argument for the printf function. (For a description of the format and arguments, seeprintf in the Run-Time Library Reference.) A null character is appended to the end of the characters written.For more information, seesprintf in the Run-Time Library Reference.ExampleCString str;str.Format(_T("Floating point: %.2f\n"), 12345.12345);
_tprintf("%s", (LPCTSTR) str);str.Format(_T("Left-justified integer: %.6d\n"), 35);
_tprintf("%s", (LPCTSTR) str);str.Format(IDS_SCORE, 5, 3);
_tprintf("%s", (LPCTSTR) str);
OutputIf the application has a string resource with the identifier IDS_SCORE that contains the string "Penguins: %d\nFlyers : %d\n", the above code fragment produces this output:Floating point: 12345.12
Left-justified integer: 000035
Penguins: 5
Flyers : 3printf Type Field Characters
The type character is the only required format field ; it appears after any optional format fields. The type character determines whether the associated argument is interpreted as a character, string, or number. The types C and S, and the behavior of c and s with printf functions, are Microsoft extensions and are not ANSI-compatible.Table R.3 printf Type Field CharactersCharacter Type Output Format
c int or wint_t When used with printf functions, specifies a single-byte character; when used with wprintf functions, specifies a wide character.
C int or wint_t When used with printf functions, specifies a wide character; when used with wprintf functions, specifies a single-byte character.
d int Signed decimal integer.
i int Signed decimal integer.
o int Unsigned octal integer.
u int Unsigned decimal integer.
x int Unsigned hexadecimal integer, using “abcdef.”
X int Unsigned hexadecimal integer, using “ABCDEF.”
e double Signed value having the form [ – ]d.dddd e [sign]ddd where d is a single decimal digit, dddd is one or more decimal digits, ddd is exactly three decimal digits, and sign is + or –.
E double Identical to the e format except that E rather than e introduces the exponent.
f double Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision.
g double Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than –4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it.
G double Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate).
n Pointer to integer Number of characters successfully written so far to the stream or buffer; this value is stored in the integer whose address is given as the argument.
p Pointer to void Prints the address pointed to by the argument in the form xxxx:yyyy where xxxx is the segment and yyyy is the offset, and the digits x and y are uppercase hexadecimal digits.
s String When used with printf functions, specifies a single-byte–character string; when used with wprintf functions, specifies a wide-character string. Characters are printed up to the first null character or until the precision value is reached.
S String When used with printf functions, specifies a wide-character string; when used with wprintf functions, specifies a single-byte–character string. Characters are printed up to the first null character or until the precision value is reached.
CString::Format
void Format( LPCTSTR lpszFormat, ... );void Format( UINT nFormatID, ... );ParameterslpszFormatA format-control string.nFormatIDThe string resource identifier that contains the format-control string. ResCall this member function to write formatted data to a CString in the same way that sprintf formats data into a C-style character array. This function formats and stores a series of characters and values in the CString. Each optional argument (if any) is converted and output according to the corresponding format specification in lpszFormat or from the string resource identified by nFormatID. The call will fail if the string object itself is offered as a parameter to Format. For example, the following code:CString str = "Some Data";
str.Format("%s%d", str, 123); // Attention: str is also used in the parameter list.will cause unpredictable results.When you pass a character string as an optional argument, you must cast it explicitly as LPCTSTR. The format has the same form and function as the format argument for the printf function. (For a description of the format and arguments, see printf in the Run-Time Library Reference.) A null character is appended to the end of the characters written.For more information, see sprintf in the Run-Time Library Reference.ExampleCString str;str.Format(_T("Floating point: %.2f\n"), 12345.12345);
_tprintf("%s", (LPCTSTR) str);str.Format(_T("Left-justified integer: %.6d\n"), 35);
_tprintf("%s", (LPCTSTR) str);str.Format(IDS_SCORE, 5, 3);
_tprintf("%s", (LPCTSTR) str);
OutputIf the application has a string resource with the identifier IDS_SCORE that contains the string "Penguins: %d\nFlyers : %d\n", the above code fragment produces this output:Floating point: 12345.12
Left-justified integer: 000035
Penguins: 5
Flyers : 3
具体应用举例:
CString mystr;
mystr.format("%d",i);
当然其中的类型格式都可以修改,具体格式可以参见MSDN!
意思是将字符串"Floating point:12345.12"格式化到buffer str中去;你可以用MessageBox(str);测试一下就知道了。关键是自己动手试一试。you will be the best!
自己琢磨,多写一些测试代码就知道了!!
比如 printf("%d + %d = %d",nA,nB,nA+nB") ;
如果nA = 3,nB = 2, 则输出为:
3 + 2 = 5
CString的Format函数跟这个差不多,都是格式化输出
比如
CString m_Name ;
m_Name.Format("aaa%d",5);
m_Name就等于 aaa5
明白?我说的比较简陋,而且严格的说是不对的,但起码容易懂