CString.format   
能帮我详细解释一下吗?
谢谢~

解决方案 »

  1.   

    给你复制MSDN上的说明,如果你略懂英文的话,应该可以明白: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, 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.
      

  2.   

    摘自msdn:
    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
      

  3.   

    CString.format 就是跟printf(),sprintf()差不多的一个函数。printf()将内容输出到屏幕,sprintf()将内容输出到字符数组中,CString.format()将内容输入到CString对象中。
      

  4.   

    用格式化数据定义一个CString对象!
    具体应用举例:
    CString mystr;
    mystr.format("%d",i);
    当然其中的类型格式都可以修改,具体格式可以参见MSDN!
      

  5.   

    CString str;str.Format(_T("Floating point: %.2f\n"), 12345.12345);
    意思是将字符串"Floating point:12345.12"格式化到buffer str中去;你可以用MessageBox(str);测试一下就知道了。关键是自己动手试一试。you will be the best!
      

  6.   

    CString.format 就是跟printf(),sprintf()差不多的一个函数。printf()将内容输出到屏幕,sprintf()将内容输出到字符数组中,CString.format()将内容输入到CString对象中。说得够清楚了。
      

  7.   

    CString.format的知识,上面已经说的太清楚了,关键要自己多用,理解,才回灵活运用。
      

  8.   

    和那个sprintf,printf 还有scanf用法差不多
      

  9.   

    多看MSDN帮助!!!
    自己琢磨,多写一些测试代码就知道了!!
      

  10.   

    这么说吧,你有没有用过C或C++中的printf函数呢?
    比如 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
    明白?我说的比较简陋,而且严格的说是不对的,但起码容易懂