函式
<cwchar>

vfwprintf

int vfwprintf (FILE* stream, const wchar_t* format, va_list arg);
從變長引數列表向流寫入格式化資料
將由 format 指向的 C 寬字串寫入 stream,並以與 printf 相同的方式替換任何 format specifier,但使用由 arg 標識的變長引數列表中的元素,而不是額外的函式引數。

在內部,該函式從 arg 標識的列表中檢索引數,如同對其使用了 va_arg 一樣,因此 arg 的狀態很可能會在呼叫後被改變。

無論如何,arg 應該在呼叫之前的某個時刻由 va_start 初始化,並且預計在呼叫之後的某個時刻由 va_end 釋放。

檔案中寬字元的外部表示形式是多位元組字元:這些字元的獲取方式就好像呼叫了 wcrtomb 來轉換每個寬字元(使用 stream 的內部 mbstate_t 物件)。

這是 vfprintf(位於 <cstdio>)的 寬字元 等效函式。

引數

stream
指向一個 FILE 物件的指標,該物件標識一個輸出流。
該流不應有任何朝向,或應是寬字元朝向的(對流的第一個 I/O 操作決定了它是位元組朝向還是寬字元朝向,請參閱 fwide)。
format
C 寬字串,包含一個格式字串,其遵循與 printfformat 相同的規範(詳情請參閱 printf)。
請注意,所有格式說明符的含義與 printf 中相同;因此,%lc應用於寫入一個寬字元(而不是%c),同樣%ls應用於寬字串(而不是%s).
arg
一個標識由 va_start 初始化的可變引數列表的值。
va_list 是在 <cstdarg> 中定義的特殊型別。

返回值

成功時,返回寫入的字元總數。

如果發生寫入錯誤,則會設定錯誤指示符ferror)並返回一個負數。

如果在寫入寬字元時發生多位元組字元編碼錯誤,errno 會被設定為EILSEQ並返回一個負數。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* vfprintf example */
#include <stdio.h>
#include <stdarg.h>
#include <wchar.h>

void WriteWideFormatted (FILE * stream, const wchar_t * format, ...)
{
  va_list args;
  va_start (args, format);
  vfwprintf (stream, format, args);
  va_end (args);
}

int main ()
{
   FILE * pFile;

   pFile = fopen ("myfile.txt","w");

   WriteWideFormatted (pFile,L"Call with %d variable argument.\n",1);
   WriteWideFormatted (pFile,L"Call with %d variable %ls.\n",2,L"arguments");

   fclose (pFile);

   return 0;
}

此示例演示了WriteWideFormatted可以用不同數量的引數來呼叫,這些引數又會傳遞給vfwprintf函式。
myfile.txt將包含

myfile.txt
Call with 1 variable argument. 
Call with 2 variable arguments.

另見