Export (0) Print
Expand All

CStringT::Format 

Writes formatted data to a CStringT in the same way that sprintf formats data into a C-style character array.


void __cdecl Format(
   UINT nFormatID,
   [, argument]...
);
void __cdecl Format(
   PCXSTR pszFormat,
   [, argument]...
);

Parameters

nFormatID

The string resource identifier that contains the format-control string.

pszFormat

A format-control string.

argument

Optional arguments.

This function formats and stores a series of characters and values in the CStringT. Each optional argument (if any) is converted and output according to the corresponding format specification in pszFormat 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 will cause unpredictable results:

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

For more information, see Format Specification Fields: printf and wprintf Functions and sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l.

// cstringt_format.cpp
#include <string.h>
#include <atlstr.h>

int main(int argc, char* argv[])
{
    //typedef CStringT< TCHAR, StrTraitATL< TCHAR > > CAtlString;
    CAtlString str;

    str.Format(_T("Floating point: %.2f\n"), 12345.12345);
    _tprintf_s("%s", (LPCTSTR) str);

    str.Format(_T("Left-justified integer: %.6d\n"), 35);
    _tprintf_s("%s", (LPCTSTR) str);


    return 0;
}

Output

Floating point: 12345.12
Left-justified integer: 000035

Community Additions

ADD
Show:
© 2014 Microsoft