Platform SDK: Windows Management Instrumentation

CHString::Format

The Format method formats and stores a series of characters and values in a CHString string.

void Format(
  LPCWSTR lpszFormat, ... )
throw ( CHeap_Exception );

void Format(
  UINT nFormatID, ... )
throw ( CHeap_Exception );

Parameters

lpszFormat
Format-control string.
nFormatID
String resource identifier that contains the format control string.

Remarks

The Format method writes formatted data to a CHString string in the same way that sprintf formats data into a C-style character array. 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.

If the string object is offered as a parameter to Format, the call fails. For example, the following code will cause unpredictable results.

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

Note  When you pass a character string as an optional argument, you must cast it explicitly as LPCWSTR. The format argument has the same form and function as the format argument for the printf function. A NULL character is appended to the end of the written characters.

Example

The following example demonstrates the use of CHString::Format:

CHString str;

str.Format(L"Floating point: %.2f\n", 12345.12345);
printf("%S", (LPCWSTR) str);

str.Format(L"Left-justified integer: %d\n", 35);
printf("%S", (LPCWSTR) str);

str.Format(IDS_SCORE, 5, 3);
printf("%S", (LPCWSTR) str);

If the application has a string resource with the IDS_SCORE identifier and if that resource contains the string, "Penguins: %d\nFlyers : %d\n", the preceding code fragment produces the following output:

Floating point: 12345.12
Left-justified integer: 35
Penguins: 5
Flyers  : 3

Requirements

  Windows NT/2000: Requires Windows NT 4.0 SP4 or later.
  Windows 95/98: Requires Windows 95 OSR2 or later.
  Header: Declared in Chstring.h.
  Library: Use Framedyn.lib.

See Also

CHString::GetBuffer, CHString::FormatV