fwprintf ()- 将数据格式化为宽字符并写入流

格式

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

语言级别

ANSI

线程安全

语言环境敏感

如果在编译命令上指定了 LOCALETYPE (*LOCALEUCS2) 或 LOCALETYPE (*LOCALELEUTF) ,那么此函数的行为可能受当前语言环境的 LC_CTYPE 和 LC_NUMERIC 类别的影响,也可能受当前语言环境的 LC_UNI_CTYPE 和 LC_UNI_NUMERIC 类别的影响。 当在编译命令上指定 LOCALETYPE (*CLD) 时,此功能不可用。 有关更多信息,请参阅 了解 CCSID 和语言环境

集成文件系统界面

当在编译命令上指定 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);
}
 

相关信息