fprintf -- Write Formatted Data to a Stream

Format

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

Language Level: ANSI, POSIX, XPG4, Extension
fprintf formats and writes a series of characters and values to the output stream. fprintf converts each entry in argument-list, if any, and writes to the stream according to the corresponding format specification in the format-string.

The format-string has the same form and function as the format-string argument for printf. See printf -- Print Formatted Characters for a description of the format-string and the argument-list.

In extended mode, fprintf also converts floating-point values of NaN and infinity to the strings "NAN or "nan" and "INFINITY" or "infinity". The case and sign of the string is determined by the format specifiers.

If you specify a null string for the %s or %ls format specifier, fprintf prints (null). (In previous releases of the C/C++ run-time library, printf produced no output for a null string.)

Return Value
fprintf returns the number of bytes printed or a negative value if an output error occurs.

Example
This example sends a line of asterisks for each integer in the array count to the file myfile. The number of asterisks printed on each line corresponds to an integer in the array.

#include <stdio.h>
#define FILENAME "myfile.dat"
int count [10] = {1, 5, 8, 3, 0, 3, 5, 6, 8, 10};
int main(void)
{
   int i,j;
   FILE *stream;
   stream = fopen("FILENAME", "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);
   return 0;
   /****************************************************
      The output data file should contain:
      *
      *****
      ********
      ***
      ***
      *****
      ******
      ********
      **********
   *****************************************************/
}


_cprintf -- Print Characters to Screen
fscanf -- Read Data from a Stream
printf -- Print Formatted Characters
sprintf -- Print Formatted Data to Buffer
vfprintf -- Print Argument Data to Stream
vprintf -- Print Argument Data
vsprintf -- Print Argument Data to Buffer
vswprintf -- Format and Write Wide Characters to Buffer
<stdio.h>