Name

fprintf

Synopsis

Writes formatted output to an open file

#include <stdio.h>
intfprintf( FILE * restrict fp, const char * restrict format, ... );

The fprintf() function is similar to printf(), but writes its output to the stream specified by fp rather than to stdout.

Example

FILE *fp_log;
time_t sec;

fp_log = fopen("example.log", "a");
if ( fp != NULL)
{
  time(&sec);fprintf( fp_log, "%.24s Opened log file.\n", ctime( &sec ) );
}

This code appends the following output to the file example.log:

Wed Dec  8 21:10:43 2004 Opened log file.

See Also

printf(), sprintf(), snprintf(), declared in stdio.h; vprintf(), vfprintf(), vsprintf(), vsnprintf(), declared in stdio.h and stdarg.h; the wide-character functions wprintf(), fwprintf(), swprintf(), declared in stdio.h and wchar.h; vwprintf(), vfwprintf(), and vswprintf(), declared in stdio.h, wchar.h, and stdarg.h; the scanf() input functions. Argument conversion in the printf() family of functions is described under printf() in this chapter.

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.