Function vfprintf () in C
The int vfprintf function (FILE * stream, const char * format, va_list arg) in the Standard C library sends the formatted output to a stream using a parameter list.
The int vfprintf function (FILE * stream, const char * format, va_list arg) in the Standard C library sends the formatted output to a stream using a parameter list.
Declare the function vfprintf () in C
Below is the declaration for vfprintf () function in C:
int vfprintf ( FILE * stream , const char * format , va_list arg )
Parameters
stream - This is the pointer to a FILE object that identifies the Stream.
format - This is the string containing the text to be written to Stream. It may optionally contain formatting tags that can be embedded that are replaced by values defined in subsequent additional parameters and formatted as required. Prototype tags of the format % [flags] [width] [. Precision] [length] specifier , explained as follows:
Character specifier Character d or i Decimal integer with the sign e Scientific symbol (mantissa / exponent) uses the character e E Scientific notation (mantissa / exponent) using characters E f Actual floating point number Decimal g Short use of% e or% f G Compact use of% E or% fo Octal number signed s Character string u Unsigned decimal number x Hexadecimal integers mark X Unsigned hexadecimal integers (uppercase letters) p Pointer address n Do not print anything% charactersflags Description - Align left into the given field width. The alignment must be the default + Forced to precede the result with a plus or minus (+ or -) even with positive numbers. By default, only negative numbers are preceded by a - (space). If no symbols are written, then a space will be inserted before the value # Used with the specifier o, x or X. The value is preceded by 0, 0x or 0X corresponding to values other than 0. Using with e, E and f, it forces the recorded output to acquire a decimal pointer even if not any number followed. By default, if no digit follows, no decimal pointer is written. Use with g or G, the result is the same as e or E but the zeroes at the end do not have to be removed from the left 0 pad (left-pad) of the number with the 0s instead of the spaces
width Description (number) The minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with spaces. The value is not cut even if the result is too large. The width is not specified in the format format string, but as an additional integer value parameter preceding that parameter must be formatted
.precision Description.number For Integer Specifiers (d, i, o, u, x, X) - then Precision specifies the minimum number of digits to be written. If the recorded value is shorter than this number, the result is padded with 0 at the beginning. Value is not cut even if the result is longer. A precision of 0 means that no character is written to the value 0. For e, E and f specifier: this is the number of digits to be printed after the decimal point. With g and G specifier: this is the maximum number of digits to be printed. With s specifier: this is the minimum number of characters to be printed. By default, all characters are printed until the last null character is encountered. With type c: it has no effect. When no precision is specified, the default is 1. If the period is specified without a clear precision value, 0 is assumed. * Precision is not specified in the format format string, but as an additional raw value parameter precedes that parameter that must be formatted
length Description The parameter is interpreted as a short int or unsigned short int (applied only to integer specifiers: i, d, o, u, x and X) l Parameters are interpreted as a long int or unsigned long int for integer specifier (i, d, o, u, x and X), and as a wide char or wide char string for specifiers are c and s L The parameter is interpreted as a long double (applied only to floating point specifier: e, E, f, g and G)
Additional parameters - Depending on the format format string, this function may have an additional parameter array, each containing a value to be inserted in place of each% -tag specified in the format parameter, if. This number of parameters should be the same number as% -tags that expect a value.
Returns the value
If successful, the total number of characters recorded will be returned, otherwise a negative number will be returned.
For example
The following program C illustrates the usage of the vfprintf () function in C:
#include #include void WriteFrmtd ( FILE * stream , char * format , .) { va_list args ; va_start ( args , format ); vfprintf ( stream , format , args ); va_end ( args ); } int main () { FILE * fp ; fp = fopen ( "baitapc.txt" , "w" ); WriteFrmtd ( fp , "Diem %d cho chat luong n" , 10 ); fclose ( fp ); return ( 0 ); }
Compiling and running the above program will open the baitpc.txt to write to the current directory and will write the following:
Now monitor the above file contents by using the following C program:
#include int main () { FILE * fp ; int c ; fp = fopen ( "baitapc.txt" , "r" ); while ( 1 ) { c = fgetc ( fp ); if ( feof ( fp ) ) { break ; } printf ( "%c" , c ); } fclose ( fp ); return ( 0 ); }
Compiling and running the above C program will produce the following results:
According to Tutorialspoint
Previous lesson: sprintf () function in C
Next lesson: Function vprintf () in C
You should read it
Maybe you are interested
The whole scene of meteorites hitting the Earth during the past 33 years, why can't we feel it? The most beautiful meteor shower of the year and amazing astronomical phenomena in August NASA reveals its latest snapshot of the Martian surface with a resolution of 1.8 billion pixels Is Proxima b our 'neighbor' planet? Why have scientists found Proxima b - '2nd Earth' until now? The mystery of the numbers of Proxima b: The 'Second Earth' on the planet may exist life