Export (0) Print
Expand All
div
Expand Minimize

fprintf, fwprintf

Print formatted data to a stream.

int fprintf( 
   FILE *stream,
   const char *format [,
   argument ]...
);
int fwprintf( 
   FILE *stream,
   const wchar_t *format [,
   argument ]...
);

Parameters

stream
Pointer to FILE structure.
format
Format-control string.
argument
Optional arguments.

Return Value

fprintf returns the number of bytes written. fwprintf returns the number of wide characters written. Each of these functions returns a negative value instead when an output error occurs.

Remarks

fprintf formats and prints a series of characters and values to the output stream. Each function argument (if any) is converted and output according to the corresponding format specification in format. For fprintf, the format argument has the same syntax and use that it has in printf.

fwprintf is a wide-character version of fprintf; in fwprintf, format is a wide-character string. These functions behave identically otherwise.

Security Note   Ensure that format is not a user-defined string.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_ftprintf fprintf fprintf fwprintf

For more information, see Format Specifications.

Requirements

Function Required header Compatibility
fprintf <stdio.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
fwprintf <stdio.h> or <wchar.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example

// crt_fprintf.c
/* This program uses fprintf to format various
 * data and print it to the file named FPRINTF.OUT. It
 * then displays FPRINTF.OUT on the screen using the system
 * function to invoke the operating-system TYPE command.
 */

#include <stdio.h>
#include <process.h>

FILE *stream;

int main( void )
{
   int    i = 10;
   double fp = 1.5;
   char   s[] = "this is a string";
   char   c = '\n';

   stream = fopen( "fprintf.out", "w" );
   fprintf( stream, "%s%c", s, c );
   fprintf( stream, "%d\n", i );
   fprintf( stream, "%f\n", fp );
   fclose( stream );
   system( "type fprintf.out" );
}

Output

this is a string
10
1.500000

See Also

Stream I/O Routines | _cprintf | fscanf | sprintf | Run-Time Routines and .NET Framework Equivalents

Show:
© 2014 Microsoft