int fprintf (FILE * stream , const char * format [ , argument , ...] );
Print formatted data to a stream.
Prints to the specified stream a sequence of arguments formatted as the format argument specifies.
|Signed decimal integer||392|
|Scientific notation (mantise/exponent) using e character||3.9265e2|
|Scientific notation (mantise/exponent) using E character||3.9265E2|
|Decimal floating point||392.65|
|Use shorter %e or %f||392.65|
|Use shorter %E or %f||392.65|
|String of characters||sample|
|Unsigned decimal integer||7235|
|Unsigned hexadecimal integer||7fa|
|Unsigned hexadecimal integer (capital letters)||7FA|
|Address pointed by the argument||B800:0000|
|Nothing printed. The argument must be a pointer to integer where the number of characters written so far will be stored.|
|Left align within the given width. (right align is the default).|
|Forces to preceed the result with a sign (+ or -) if signed type. (by default only - (minus) is printed).|
|If the argument is a positive signed value, a blank is inserted before the number.|
|Used with o, x or X type the value is preceeded with 0, 0x or 0X respectively if non-zero.|
Used with e, E or f forces the output value to contain a decimal point even if only zeros follow.
Used with g or G the result is the same as e or E but trailing zeros are not removed.
|Minimum number of characters to be printed. If the value to be printed is shorter than this number the result is padded with blanks. The value is never truncated even if the result is larger.|
|Same as above but filled with 0s instead of blanks.|
|The width is not specified in the format string, it is specified by an integer value preceding the argument thas has to be formatted.|
for d, i, o, u, x, X types: precision specifies the minimum number of decimal digits to be printed. If the value to be printed is shorter than this number the result is padded with blanks. The value is never truncated even if the result is larger.(if nothing specified default is 1).|
for e, E, f types: number of digits to be printed after de decimal point. (if nothing specified default is 6).
for g, G types : maximum number of significant numbers to be printed.
for s type: maximum number of characters to be printed. (default is to print until first null character is encountered).
for c type : (no effect).
|meaning (affects on how arguments are interpreted by the function)|
|argument is interpreted as short int (integer types).|
|argument is interpreted as long int (interger types) or double (floating point types).|
|argument is interpreted as long double (floating point types).|
On success, the total number of characters printed is returned.
On error, a negative number is returned.
/* fprintf example */
FILE * pFile;
char name ;
pFile = fopen ("myfile.txt","w");
for (n=0 ; n<3 ; n++)
puts ("please, enter a name: ");
fprintf (pFile, "Name %d [%-10.10s]\n",n,name);
This example prompts 3 times the user for a name and then writes them to myfile.txt each one in a line with a fixed length (a total of 19 characters + newline).
Two format tags are used:
%d : Signed decimal integer
%-10.10s : left aligned (-), minimum of 10 characters (10), maximum of 10 characters (.10), String (s) .
Assuming that we have entered John, Jean-Francois and Yoko as the 3 names, myfile.txt would contain:
Name 1 [John ]
Name 2 [Jean-Franc]
Name 3 [Yoko ]