fprintf() — Write Formatted Data to a Stream

Format

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

Language Level: ANSI

Threadsafe: Yes.

Locale Sensitive: The behavior of this function might be affected by the LC_CTYPE and LC_NUMERIC categories of the current locale. The behavior might also be affected by the LC_UNI_CTYPE category of the current locale if LOCALETYPE(*LOCALEUCS2) or LOCALETYPE(*LOCALEUTF) is specified on the compilation command. For more information, see Understanding CCSIDs and Locales.

Description

The fprintf() function formats and writes a series of characters and values to the output stream. The fprintf() function 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 the printf() function.

Return Value

The fprintf() function returns the number of bytes that are printed or a negative value if an output error occurs.

For information about errno values for fprintf(), see printf() — Print Formatted Characters.

Example that uses fprintf()

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

#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:  ***************
 
*
*****
********
***
 
***
*****
******
********
**********
*/

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]