fprintf ()- 将格式化数据写入流

格式

#include <stdio.h>
int fprintf(FILE *stream, const char *format-string, argument-list);

语言级别

ANSI

线程安全

语言环境敏感

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

描述

fprintf() 函数将一系列字符和值格式化并写入输出 fprintf() 函数会转换 argument-list中的每个条目 (如果有) ,并根据 format-string中相应的格式规范写入流。

format-string 具有与 printf() 函数的 format-string 自变量相同的格式和函数。

返回值

如果发生输出错误, fprintf() 函数将返回打印的字节数或负值。

有关 fprintf()的 errno 值的信息,请参阅 printf ()-打印格式化字符

示例

此示例将数组 count 中每个整数的一行星号发送到文件 myfile。 每行上打印的星号数对应于数组中的整数。
#include <stdio.h>
 
int count [10] = {1, 5, 8, 3, 0, 3, 5, 6, 8, 10};
 
int main(void)
{
   int i,j;
   FILE *stream;
 
   stream = fopen("mylib/myfile", "w");
                  /* Open the stream for writing */
   for (i=0; i < sizeof(count) / sizeof(count[0]); i++)
   {
      for (j = 0; j < count[i]; j++)
         fprintf(stream,"*");
                  /* Print asterisk              */
         fprintf(stream,"\n");
                  /* Move to the next line       */
   }
   fclose (stream);
}
 
/*******************  Output should be similar to:  ***************
 
*
*****
********
***
 
***
*****
******
********
**********
*/

相关信息