fgetsReads a line of characters from the specified file into the buffer provided by the caller, gets reading a line of characters from the standard input into the buffer provided by the caller.
Char *fgets (char *s, int size, FILE *stream); Char *gets (char *s);
Return value: When it is successful, it points to which pointer is returned, error or NULL when reading to the end of the file;
getsThe interface design of the function is problematic, the user provides a buffer, but can not specify the size of the buffer, it is likely to lead to buffer overflow error, the function is strcpy more dangerous than strcpy the input and output from within the program, as long as the programmer is careful to avoid problems, and gets The read input comes directly from outside the program, the user may provide arbitrary long strings through standard input, and the programmer cannot avoid gets the buffer overflow error caused by the function, so the only way is not to use it.
fgets function, parameter s is the first address of the buffer, is the length of the buffer that is read from the file referred to by stream to a line ending with , including the Code class= "literal", "/n" ) is stored in the buffer s and Adds a makes up the complete string. If a line in the file is too long, fgets read from the file size-1 characters have not been read ' n ' , put the read size-1 characters and a '/0 ' The character is stored in a buffer, and the remaining half of the line in the file can continue to be read the next time fgets is called. If a fgets call is read into a number of words specifier arrive at the end of the file, the read string plus '/0 ' is stored in the buffer and returned, If you call fgets then return null to determine if the end of the file is read.
Note that, for fgets speaking, ‘/n‘ it is a special character, and ‘/0‘ nothing special, if read as a ‘/0‘ normal character read in. If there are ‘/0‘ characters (or 0x00 bytes) in the file, fgets it is not possible to determine whether the buffer ‘/0‘ is read from the file or fgets automatically added by the Terminator, so it is fgets suitable only for reading text files and not for reading binary files. And all characters in the text file should be visible characters and cannot have ‘/0‘ .
Gets and Fgets