Monday, February 20, 2012

C Language - fprintf () : write formatted output to stream

fprintf

function
<cstdio>
int fprintf ( FILE * stream, const char * format, ... );
Write formatted output to stream
Writes to the specified stream a sequence of data formatted as the format argument specifies. After the format parameter, the function expects at least as many additional arguments as specified in format.

Parameters

stream
Pointer to a FILE object that identifies the stream.
format
C string that contains the text to be written to the stream. It can optionally contain embedded format tags that are replaced by the values specified in subsequent additional arguments and formatted as requested. The number of arguments following the format parameters should at least be as much as the number of format tags. The format tags follow this prototype: %[flags][width][.precision][length]specifier Where specifier is the most significant one and defines the type and the interpretation of the value of the coresponding argument:
specifierOutputExample
cCharactera
d or iSigned decimal integer392
eScientific notation (mantissa/exponent) using e character3.9265e+2
EScientific notation (mantissa/exponent) using E character3.9265E+2
fDecimal floating point392.65
gUse the shorter of %e or %f392.65
GUse the shorter of %E or %f392.65
oSigned octal610
sString of characterssample
uUnsigned decimal integer7235
xUnsigned hexadecimal integer7fa
XUnsigned hexadecimal integer (capital letters)7FA
pPointer addressB800:0000
nNothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored.
%A % followed by another % character will write % to the stream.
The tag can also contain flags, width, .precision and modifiers sub-specifiers, which are optional and follow these specifications:
flagsdescription
-Left-justify within the given field width; Right justification is the default (see width sub-specifier).
+Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign.
(space)If no sign is going to be written, a blank space is inserted before the value.
#Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero.
Used with e, E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written.
Used with g or G the result is the same as with e or E but trailing zeros are not removed.
0Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see width sub-specifier).
widthdescription
(number)Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.
*The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
.precisiondescription
.numberFor integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0.
For e, E and f specifiers: this is the number of digits to be printed after de decimal point.
For g and G specifiers: This is the maximum number of significant digits to be printed.
For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.
For c type: it has no effect.
When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed.
.*The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
lengthdescription
hThe argument is interpreted as a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x and X).
lThe argument is interpreted as a long int or unsigned long int for integer specifiers (i, d, o, u, x and X), and as a wide character or wide character string for specifiers c and s.
LThe argument is interpreted as a long double (only applies to floating point specifiers: e, E, f, g and G).
additional arguments
Depending on the format string, the function may expect a sequence of additional arguments, each containing one value to be inserted instead of each %-tag specified in the format parameter, if any. There should be the same number of these arguments as the number of %-tags that expect a value.

Return Value

On success, the total number of characters written is returned.
On failure, a negative number is returned.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* fprintf example */
#include <stdio.h>

int main ()
{
   FILE * pFile;
   int n;
   char name [100];

   pFile = fopen ("myfile.txt","w");
   for (n=0 ; n<3 ; n++)
   {
     puts ("please, enter a name: ");
     gets (name);
     fprintf (pFile, "Name %d [%-10.10s]\n",n,name);
   }
   fclose (pFile);

   return 0;
}


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 ten characters (10), maximum of ten characters (.10), String (s) .
Assuming that we have entered John, Jean-Francois and Yoko as the 3 names, myfile.txt would contain:

myfile.txt
Name 1 [John      ] 
Name 2 [Jean-Franc] 
Name 3 [Yoko      ] 

For more examples on formatting see printf.

No comments:

Post a Comment