Function fprintf () in C
The int fprintf function (FILE * stream, const char * format, .) in standard C library sends formatted output to a Stream.
Declare the function fprintf () in C
Below is the declaration for fprintf () in C:
int fprintf ( FILE * stream , const char * format , .)
Parameters
stream - This is the pointer to a FILE object that identifies the Stream.
format - This is a string that contains the text 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 zero at the end is not removed 0
The left side 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 written is returned, otherwise a negative number will be returned.
For example
The following program C illustrates the usage of the fprintf () function in C:
#include #include int main () { FILE * fp ; fp = fopen ( "baitapc.txt" , "w+" ); fprintf ( fp , "%s %s %s %s" , "Chung" , "toi" , "la" , "QTMTeam" ); fclose ( fp ); return ( 0 ); }
Compiling and running the above program will create a baitapc.txt
Now use the following C program to monitor the file contents above:
#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 ); }
Compile and run the above C program to see the results:
According to Tutorialspoint
Previous article: Function tmpnam () in C
Next lesson: The function printf () in C
You should read it
Maybe you are interested
Quiz: Only a genius can find the number 20 in this 'sea' of number 2 in less than 6 seconds
Looking Back at Microsoft Edge in 2024: The Impressive Numbers
How to use the Round function in Excel to round numbers and process data
The Constant Pi (π): History of the Discovery and Application in Mathematics of the Mysterious Number Pi
How to generate random numbers in Excel
Instructions for converting numbers to words in Excel