fwprintf ()- 將資料格式化為寬字元並寫入串流

格式

#include <stdio.h>
#include <wchar.h>
int fwprintf(FILE *stream, const wchar_t *format, argument-list);

語言層次

ANSI

安全執行緒

區分語言環境

此函數的行為可能受到現行語言環境的 LC_CTYPE 及 LC_NUMERIC 種類的影響,也可能受到現行語言環境的 LC_UNI_CTYPE 及 LC_UNI_NUMERIC 種類的影響 (如果在編譯指令上指定 LOCALETYPE (*LOCALEUCS2) 或 LOCALETYPE (*LOCALEUTF))。 在編譯指令上指定 LOCALETYPE (*CLD) 時,無法使用此函數。 如需相關資訊,請參閱 瞭解 CCSID 及語言環境

Integrated File System 介面

在編譯指令上指定 SYSIFCOPT (*NOIFSIO) 時,無法使用此函數。

寬字元函數

如需相關資訊,請參閱 寬字元

說明

fwprintf() 函數將輸出寫入 stream所指向的串流,由 format所指向的寬字串控制。 格式字串指定如何轉換輸出的後續引數。

fwprintf() 函數會根據格式中對應的寬字元格式指定元,來轉換 argument-list 中的每一個項目。

如果格式的引數不足,則未定義行為。 如果在引數保留時用盡格式,則 fwprintf() 函數會評估多餘的引數,但否則會忽略它們。 當 fwprintf() 函數發現格式字串結尾時傳回。

格式包含零個以上指引: 一般寬字元 (非%) 及轉換規格。 轉換規格在處理時就如同在格式字串中被寬字元字串取代一樣。 寬字元字串是提取零個以上後續引數的結果,然後根據對應的轉換指定元來轉換它們 (如果適用的話)。 然後, fwprintf() 函數會將展開的寬字元格式字串寫入輸出串流。

fwprintf() 函數的格式與 printf()的格式字串具有相同的格式及函數,但下列例外:
  • %c (不含 l 字首) 會將整數引數轉換為 wchar_t ,如同呼叫 btowc() 函數一樣。
  • %s (不含 l 字首) 會將多位元組字元陣列轉換為 wchar_t 陣列,如同呼叫 mbrtowc() 函數一樣。 除非精準度指定較短的輸出,否則陣列會寫入至 (但不包括) 終止空值字元。
  • %ls 和 %S 寫入 wchar_t 的陣列。 除非精準度指定較短的輸出,否則陣列會寫入至 (但不包括) 結尾空值字元。
  • 為 %c、 %s、%ls 及 %S 指定的任何寬度或精準度都指出字元數,而非位元組數。

如果轉換規格無效,則未定義行為。

如果任何引數是聯集或聚集 (使用 %s 轉換的 char 類型陣列、使用 %ls 轉換的 wchar_t 類型陣列或使用 %p 轉換的指標除外) ,則未定義行為。

在任何情況下,不存在或欄位寬度較小會導致欄位截斷; 如果轉換結果比欄位寬度更寬,則會展開欄位以包含轉換結果。

附註: 當您撰寫寬字元時,應該以二進位模式開啟檔案,或使用 o_ccsidcodepage 參數開啟檔案。 這可確保不會在寬字元上進行轉換。

回覆值

fwprintf() 函數會傳回傳輸的寬字元數。 如果發生輸出錯誤,則會傳回負值。

範例

#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int count [10] = {1, 5, 8, 3, 0, 3, 5, 6, 8, 10};
int main(void)
{
   int i,j;
   FILE *stream;                      /* Open the stream for writing */ 
   if (NULL == (stream = fopen("/QSYS.LIB/LIB.LIB/WCHAR.FILE/WCHAR.MBR","wb")))
      perror("fopen error"); 
   for (i=0; i < sizeof(count) / sizeof(count[0]); i++)
   {
      for (j = 0; j < count[i]; j++)
         fwprintf(stream, L"*");      /* Print asterisk              */ 
         fwprintf(stream, L"\n");     /* Move to the next line       */ 
   }
   fclose (stream);
}
/* The member WCHAR of file WCHAR will contain:
   *
   *****
   ********
   ***
   ***
   *****
   ******
   ********
   **********
 */                             

Unicode 範例

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
/* This program is compile LOCALETYPE(*LOCALEUCS2) and             */
/* SYSIFCOPT(*IFSIO)                                               */
int main(void)
{
    FILE *stream;
    wchar_t wc = 0x0058;     /* UNICODE X */
    char c1 = 'c';
    char *s1 = "123";
    wchar_t ws[4];
    setlocale(LC_ALL,
     "/QSYS.LIB/EN_US.LOCALE"); /* a CCSID 37 locale */
    ws[0] = 0x0041;        /* UNICODE A   */
    ws[1] = (wchar_t)0x0042;        /* UNICODE B   */
    ws[2] = (wchar_t)0x0043;        /* UNICODE C   */
    ws[3] = (wchar_t)0x0000;

    stream = fopen("myfile.dat", "wb+");

    /* lc and ls take wide char as input and just copies then  */
    /* to the file.  So the file would look like this        */
    /* after the below fwprintf statement:                   */
    /* 0058002000200020004100420043                          */
    /* 0020 is UNICODE blank                                 */

    fwprintf(stream, L"%lc   %ls",wc,ws);
    /* c and s take multibyte as input and produce UNICODE   */
    /* In this case c1 and s1 are CCSID 37 characters based  */
    /* on the setlocale above.  So the characters are        */
    /* converted from CCSID 37 to UNICODE and will look      */
    /* like this in hex after the following fwprintf         */
    /* statement:   0063002000200020003100320033             */
    /* 0063 is a UNICODE c  0031 is a UNICODE 1 and so on    */

    fwprintf(stream, L"%c   %s",c1,s1);

    /* Now lets try width and precision.  6ls means write  */
    /* 6 wide characters so we will pad with 3 UNICODE     */
    /* blanks and %.2s means write no more then 2 wide     */
    /* characters.  So we get an output that looks like    */
    /* this:  00200020002000410042004300310032             */

    fwprintf(stream, L"%6ls%.2s",ws,s1);
}
 

相關資訊