Input Function
<1>Scanf, sscanf (the input source is a string), and fscanf (the input source is a file) Functions
Return Value: return the number of successfully read items. If no project is read, 0 is returned. When it detects the end of a file, it returns EOF.
Enter the end flag:Skip the line break of the input queue and start reading with a space. When a line break occurs, a space is used to end reading,
Note: When a string is input, the comma cannot be used as a separator. When scanf reads multiple data items, if one item fails to be read, the subsequent items will not be read.
Used to read and convert a word or a standard type of mixed data.
Note: keep up with while (getchar ()! = '\ N') continue; used to delete unnecessary characters in the input queue.
Int main (INT argc, char ** argv)
{
Char * name1 = NULL;
Char name2 [10];
Int I = 0, j = 0, K = 0, L = 0;
Char * name3 = "Hello World ";
Char name4 [10];
Char name5 [10];
Char name6 [10];
I = scanf ("% d, % d, % s, % d", & J, & K, name1, & L); // J = 34, K = 45, L = 0; scanf returns 2, successfully reads J and K; name1 is null, reading error, stop reading here
J = scanf ("% s, % d", name2, & I); // "Zhou, 56" of the input queue is input to name2, scanf returns 1, do not think 56 will be assigned to I
K = sscanf (name3, "% S % s", name4, name5); // specify name3 as the input source and assign it to name4 = "hello ", name5 = "world", sscanf returns 2
L = fscanf (stdin, "% s", name6); // name6 = "Tian", fscanf returns 1
Return 0;
}/* ----- End of main ()-----*/
Running result: 34,45, Zhou, 56 [enter] Tian [enter] <2>
Gets and fgets (name, Max, FP)Return Value: returns a pointer to Char. If an error occurs or the end of a file is returned, null input is returned. If '\ n' is returned, the end of the file is read, add '\ 0' after the read string '. Gets does not read '\ n', fgets reads "\ n" NOTE: gets does not check whether the reserved storage area can accommodate the actual input data; fgets reads a maximum of MAX-1 characters from the FP File
Int main (INT argc, char ** argv)
{
Char name [10];
Char name1 [10];
Char * name2 = NULL;
Char * P1 = NULL;
Char * P2 = NULL;
Char * P3 = NULL;
P1 = fgets (name, 10, stdin); // The returned Pointer Points to name
P2 = gets (name1); // The returned Pointer Points to name1
P3 = gets (name2); // name2 has no memory space allocated, name2 has no place to put, and a segment error occurs.
Return 0;
}/* ----- End of main ()-----*/
Running result <1>:
Hello [enter] // name = "hello"
World [enter] // name1 = "world"
Zhou [enter]
Segmentation fault (core dumped)
Running result <2>:
Hello world [enter] // name = "Hello wor" name1 = "LD"
Zhou [enter]
Segmentation fault (core dumped)
<3>
Getchar () and GETC (FP)Return Value: returns the next character from the input device. The value ranges from 0 to 127. If an EOF is detected at the end of the file, it is used to enter a single character. They do not require format specifiers and only apply to characters. Note: line breaks are read without skipping the line breaks in the input queue. GETC obtains a character from the file pointed to by FP. Commonly Used: While (CH = getchar ())! = EOF) {If ('\ n' = CH) continue; // used to skip the linefeed while (getchar ()! = '\ N') continue; // used to skip unnecessary characters in the input line/* executed content */
} CH = GETC (FP)
Output Function<1> printf, sprintf (output to the array), and fprintf (output to the file pointed to by FP) are used: sprintf can output several elements to the array as strings. Note: When Using printf (), pay attention to the number of conversion specifiers. Data Types must correspond to each other; otherwise, a segment error occurs;
Int main (INT argc, char ** argv)
{
Char * STR = "Hello World ";
Char Buf [20];
Int I = 23;
Sprintf (BUF, "% s, % d", STR, I );
Fprintf (stdout, "% s, % d \ n", STR, I );
Printf ("% s \ n", STR, I );
Printf ("% s, % d \ n", STR );
Printf ("% s, % s \ n", STR, I );//The data type does not match. A segment error occurs !!!
Return 0;
}/* ----- End of main ()-----*/
Running result:
Hello world, 23
Hello World
Hello world, 4196184
Segmentation fault (core dumped)
<2> puts (name) and fputs (name, FP)
Stop output: Stop output when the null character '\ 0' is encountered.
Note: line breaks are automatically added when puts displays strings. line breaks are not automatically added when fputs displays strings.
Note that the character array does not have '\ 0'
<3> putchar (CH) prints its parameter. putc (CH, fpout) writes ch to the file pointed to by fpout.
Input and Output Functions