|
Fprintf (format the output data to a file) |
| Related functions |
Printf, fscanf, vfprintf
|
| Header file |
# Include <stdio. h>
|
| Define functions |
Int fprintf (File * stream, const char * format ,.......);
|
| Function Description |
Fprintf () will convert and format the data according to the format string, and then output the result to the file specified by the parameter stream until the string ends ('/0.
|
| Return Value |
For the format of the format string, see printf (). If the operation succeeds, the number of actual output characters is returned. If the operation fails, the value-1 is returned. The error cause is stored in errno.
|
| Example |
# Include <stdio. h> Main () { Int I = 150; Int J =-100; Double K = 3.14159; Fprintf (stdout, "% d % F % x/N", J, K, I ); Fprintf (stdout, "% 2D % * D/N", I, 2, I ); }
|
| Run |
-100 3.141590 96 150 150
|
| |
|
Fscanf (Format String input) |
| Related functions |
Scanf, sscanf
|
| Header file |
# Include <stdio. h>
|
| Define functions |
Int fscanf (File * stream, const char * format ,....);
|
| Function Description |
Fscanf () reads the string from the file stream of the parameter stream, and then converts and formats the data according to the format string. For more information about format conversion, see scanf (). The converted structure is stored in the corresponding parameter.
|
| Return Value |
If the call succeeds, the number of parameters is returned. If the call fails,-1 is returned. The error cause is stored in errno.
|
| Additional instructions |
|
| Example |
# Include <stdio. h> Main () { Int I; Unsigned Int J; Char s [5]; Fscanf (stdin, "% d % x % 5 [A-Z] % * S % F", & I, & J, S, S ); Printf ("% d % s/n", I, j, S ); }
|
| Run |
10 0x1b aaaaaaaaa bbbbbbbbbb/* input from keyboard */ 10 27 aaaaa
|
| |
|
Printf (format output data) |
| Related functions |
Scanf, snprintf
|
| Header file |
# Include <stdio. h>
|
| Define functions |
Int printf (const char * format ,.............);
|
| Function Description |
Printf () converts and formats the data according to the format string, and then writes the result to the standard output device until the string ends ('/0. The format string can contain the following three character types: 1. General text, accompanied by direct output. 2. ASCII control characters, such as/t and/n. 3. Format Conversion characters. The format is converted into a percent sign (%) and a subsequent formatted character. Generally, each % symbol must be followed by a printf () parameter (only when the % conversion character appears ), the data type to be output must be the same as the corresponding conversion character type. The general format of printf () format conversion is as follows: % (Flags) (width) (. Prec) Type Parameters enclosed in parentheses are optional parameters, while % and type are necessary. Next we will first introduce several types Integer The integer % d is converted into a signed decimal number. The % u integer is converted to an unsigned decimal number. The % O integer is converted into an unsigned octal number. The % x integer is converted to an unsigned hexadecimal number and expressed in lowercase abcdef. The % x integer is converted into an unsigned hexadecimal number, and abcdef is used to represent the floating point number. The parameter of the % F double type is converted to a decimal number and rounded to six digits below the decimal point. Parameters of the % E double type are printed in an exponential form. There is a number that is before the decimal point, the six digits are after the decimal point, and the exponent part is expressed in lowercase E. % E has the same effect as % E. The only difference is that the exponent part is expressed by an uppercase E. Parameters of the % G Double type are automatically printed in the format of % f or % E. The standard is determined based on the value to be printed and the specified number of valid digits. The difference between % G and % G is the same. The only difference is that % E format is used for exponential printing. Character and string % C Integer Parameters are converted to the unsigned char type for printing. The parameter % s pointing to the string will be output verbatim until null characters appear. % P if the parameter is a void * pointer, it is displayed in hexadecimal format. Prec has several situations 1. The minimum number of digits of a positive integer. 2. represents the number of decimal places in the floating point type 3.% G represents the maximum number of valid digits. 4.% s represents the maximum length of the string. 5. If the symbol is X, the value of the next parameter is the maximum length. Width is the minimum length of the parameter. If this column is not a value but a * symbol, it indicates that the next parameter is used as the parameter length. Flags has the following situations: # NAME? + When printing a negative number, printf () will print a negative number, and the integer will not add any negative number. This flag adds a positive sign (+) before a positive number is printed ). # This flag has different meanings based on the converted characters. When the type is O (for example, % # O), an O is printed before the octal value is printed. Before the type is X (% # X), '0x 'is printed multiple times before the hexadecimal number is printed ', before the type is E, E, F, G, or G, a numeric value is forced to print the decimal point. When the type is g or G, the zero at the end of the decimal point and decimal point is also reserved. 0 when a specified parameter exists, the parameter without a number is set to 0. The flag is disabled by default, so white spaces are usually printed.
|
| Return Value |
If the operation succeeds, the number of actual output characters is returned. If the operation fails, the value-1 is returned. The error cause is stored in errno.
|
| Example |
# Include <stdio. h> Main () { Int I = 150; Int J =-100; Double K = 3.14159; Printf ("% d % F % x/N", J, K, I ); Printf ("% 2D % * D/N", I, 2, I);/* parameter 2 will be substituted into the format *, which is the same as % 2D */ }
|
| Run |
-100 3.14159 96 150 150
|
| |
|
Sacnf (Format String input) |
| Related functions |
Fscanf, snprintf
|
| Header file |
# Include <stdio. h>
|
| Define functions |
Int scanf (const char * format ,.......);
|
| Function Description |
Scanf () converts and formats the input data according to the format string. The common format of scanf () format conversion is as follows: % [*] [Size] [l] [H] Type Parameters enclosed in parentheses are optional parameters, while % and type are necessary. * Indicates that the corresponding parameter data is ignored and not saved. Size indicates the length of data that can be input. L The input data values are saved in the long Int or double type. H. The input data values are saved in the short int type. Below we will introduce several types The data entered by % d is converted into a signed decimal number (INT ). % I the input data is converted into a signed decimal number. If the input data starts with "0x" or "0x", it indicates that the hexadecimal number is converted, if it starts with "0", it is converted to an octal number. Otherwise, it indicates decimal. The data entered by % 0 is converted into an unsigned octal number. The data entered by % u is converted into an unsigned positive integer. The data entered by % x is an unsigned hexadecimal number, which is converted and stored in the unsigned int type variable. % X is the same as % x % F indicates a signed floating point number, which is converted and stored in a float variable. % E is the same as % F % E is the same as % F % G is the same as % F The % s input data is a string ending with a space character. % C the input data is a single character. [] Read data, but only characters in parentheses are allowed. For example, [A-Z]. [^] The character after the ^ symbol that reads data but does not allow brackets, for example, [^ 0-9].
|
| Return Value |
If the call succeeds, the number of parameters is returned. If the call fails,-1 is returned. The error cause is stored in errno.
|
| Example |
# Include <stdio. h> Main () { Int I; Unsigned Int J; Char s [5]; Scanf ("% d % x % 5 [A-Z] % * S % F", & I, & J, S, S ); Printf ("% d % s/n", I, j, S ); }
|
| Run |
10 0x1b aaaaaaaa bbbbbbbbbb 10 27 aaaaa
|
| |
|
Sprintf (format string copy) |
| Related functions |
Printf, sprintf
|
| Header file |
# Include <stdio. h>
|
| Define functions |
Int sprintf (char * STR, const char * format ,.........);
|
| Function Description |
Sprintf () will convert and format the data according to the format string, and then copy the result to the string array indicated by the STR parameter until the string ends ('/0. For the format of the format string, see printf ().
|
| Return Value |
If the request succeeds, the STR string length is returned. If the request fails,-1 is returned. The error cause is stored in errno.
|
| Additional instructions |
Use this function to pay attention to stack overflow, or use snprintf () instead ().
|
| Example |
# Include <stdio. h> Main () { Char * A = "this is string !"; Char Buf [80]; Sprintf (BUF, ">>> % S </N", ); Printf ("% s". BUF ); }
|
| Run |
>>> This is string! <
|
| |
|
Sscanf (Format String input) |
| Related functions |
Scanf, fscanf
|
| Header file |
# Include <stdio. h>
|
| Define functions |
Int sscanf (const char * STR, const char * format ,........);
|
| Function Description |
Sscanf () converts and formats the STR string based on the format string. For more information about format conversion, see scanf (). The converted result is stored in the corresponding parameter.
|
| Return Value |
If the call succeeds, the number of parameters is returned. If the call fails,-1 is returned. The error cause is stored in errno.
|
| Example |
# Include <stdio. h> Main () { Int I; Unsigned Int J; Char input [] = "10 0x1b aaaaaaaa bbbbbbbb "; Char s [5]; Sscanf (input, "% d % x % 5 [A-Z] % * S % F", & I, & J, S, S ); Printf ("% d % s/n", I, j, S ); }
|
| Run |
10 27 aaaaa
|
| |
|
Vfprintf (format the output data to a file) |
| Related functions |
Printf, fscanf, fprintf
|
| Header file |
# Include <stdio. h> # Include <stdarg. h>
|
| Define functions |
Int vfprintf (File * stream, const char * format, va_list AP );
|
| Function Description |
Vfprintf () will convert and format the data according to the format string, and then output the result to the file specified by the parameter stream until the string ends ('/0. For the format of the format string, see printf (). For more information about va_list usage, see Appendix C or vprintf.
|
| Return Value |
If the operation succeeds, the number of actual output characters is returned. If the operation fails, the value-1 is returned. The error cause is stored in errno.
|
| Example |
See fprintf () and vprintf ().
|
| |
|
Vfscanf (Format String input) |
| Related functions |
Scanf, sscanf, fscanf
|
| Header file |
# Include <stdio. h>
|
| Define functions |
Int vfscanf (File * stream, const char * format, va_list AP );
|
| Function Description |
Vfscanf () reads the string from the file stream of the parameter stream, and then converts and formats the data according to the format string. For more information about format conversion, see scanf (). The converted result is stored in the corresponding parameter. For more information about va_list usage, see Appendix C or vprintf ().
|
| Return Value |
If the call succeeds, the number of parameters is returned. If the call fails,-1 is returned. The error cause is stored in errno.
|
| Example |
See fscanf () and vprintf ().
|
| |
|
Vprintf (format output data) |
| Related functions |
Printf, vfprintf, vsprintf
|
| Header file |
# Include <stdio. h> # Include <stdarg. h>
|
| Define functions |
Int vprintf (const char * format, va_list AP );
|
| Function Description |
Vprintf () works the same as printf (), and the format of the parameter is the same. Va_list is a parameter column with an indefinite number. For usage and examples, see appendix C.
|
| Return Value |
If the operation succeeds, the number of actual output characters is returned. If the operation fails, the value-1 is returned. The error cause is stored in errno.
|
| Example |
# Include <stdio. h> # Include <stdarg. h> Int my_printf (const char * format ,......) { Va_list AP; Int retval; Va_start (AP, format ); Printf ("my_printf ():"); Retval = vprintf (format, AP ); Va_end (AP ); Return retval; } Main () { Int I = 150, j =-100; Double K = 3.14159; My_printf ("% d % F % x/N", J, K, I ); My_printf ("% 2D % * D/N", I, 2, I ); }
|
| Run |
My_printf ():-100 3.14159 96 My_printf (): 150 150
|
| |
|
Vscanf (Format String input) |
| Related functions |
Vsscanf, vfscanf
|
| Header file |
# Include <stdio. h> # Include <stdarg. h>
|
| Define functions |
Int vscanf (const char * format, va_list AP );
|
| Function Description |
Vscanf () converts and formats the input data according to the format string. For more information about format conversion, see scanf (). The converted result is stored in the corresponding parameter. For more information about va_list usage, see Appendix C or vprintf.
|
| Return Value |
If the call succeeds, the number of parameters is returned. If the call fails,-1 is returned. The error cause is stored in errno.
|
| Example |
See scanf () and vprintf ().
|
| |
|
Vsprintf (format string copy) |
| Related functions |
Vnsprintf, vprintf, snprintf
|
| Header file |
# Include <stdio. h>
|
| Define functions |
Int vsprintf (char * STR, const char * format, va_list AP );
|
| Function Description |
Vsprintf () will convert and format the data according to the format string, and then copy the result to the string array indicated by the STR parameter until the end of the string ('/0') appears. For the format of the format string, see printf (). For more information about va_list usage, see Appendix C or vprintf.
|
| Return Value |
If the request succeeds, the STR string length is returned. If the request fails,-1 is returned. The error cause is stored in errno.
|
| Example |
See vprintf () and vsprintf ().
|
| |
|
Vsscanf (Format String input) |
| Related functions |
Vscanf, vfscanf
|
| Header file |
# Include <stdio. h>
|
| Define functions |
Int vsscanf (const char * STR, const char * format, va_list AP );
|
| Function Description |
Vsscanf () converts and formats the STR string based on the format string. For the format conversion format, see Appendix C or vprintf.
|
| Return Value |
If the call succeeds, the number of parameters is returned. If the call fails,-1 is returned. The error cause is stored in errno.
|
| Example |
See sscanf () and vprintf (). |