CString::FormatV

void FormatV( LPCTSTR lpszFormat, va_list argList );

参数:
lpszFormat一个格式控制字符串。
argList要传递的参数列表。

说明:
此成员函数用来将格式化数据和一个参数变量列表写入一个CString中,其方法就像vsprintf函数向一个C-风格的字符数组中格式化输出数据一样。这个成员函数在CString中格式化并存储一系列字符和值。根据lpszFormat中指定的格式,函数中的每一个可选参数都被转换并输出。
如果此字符串对象本身是作为FormatV的一个次数,则调用将失败。例如象下面的代码:
CString str = "Some Data";
str.FormatV("%s%d",str, 123); //注意:在次数列表中也使用了str。
将导致不可预期的结果。
更多的信息,参见“Microsoft Visual C++ 6.0运行库参考”中的vsprintf。

示例:
// 使用CString::FormatV,你可以像下面这样来编写函数:
void WriteLogEntry( CStdioFile& refFile, LPCTSTR pstrFormat, ... )
{
  CTime timeWrite;
  timeWrite = CTime:GetCurrentTime( );
  // 输出时间
  CString str = timeWrite.Format ( "%d %b %y %H:%M:%S -");
  refFile.Write( str, str.GetLength( ));
  // 格式化并输出给定的数据
  va_list args;
  va_start( args, pstrFormat );
  str.FormatV( pstrFormat, args );
  refFile.Write( str, str.GetLength( ) );
  // 插入一个换行符
  refFile.Write( "n", 1 );
  return;
}
你可以用任意个数的参数来调用上面的函数,例如:
WriteLogEntry( fileLog , "Program started*);
WriteLogEntry( fileLog , "Processed %d bytes* 91341 );
WriteLogEntry( fileLog , "%d error(s) found in %d line(s)", 10, 1351 );
WriteLogEntry( fileLog , "Program completed");
上面的例子将把输出添加到你的fileLog文件,就像下面给出的一样:
17 Apr 97 12:34:53 - Program started
17 Apr 97 12:34:59 - Processed 91341 bytes
17 Apr 97 12:35:22 - 10 error(s) found in 1351 line(s)
17 Apr 97 12:35:23 - Program completed

请参阅:CString::Format, va_start