Home > Article > Backend Development > What is the usage of fgets function in C language?
c language fgets function usage: fgets() function is used to read a line or a specified number of characters from the file stream.
The syntax is:
char * fgets(char * string, int size, FILE * stream);
Parameter description:
s: Character pointer, pointing to the buffer that stores the read data District address.
n: Read n-1 characters from the stream
stream: Points to the read stream.
Return value:
(1) When n<=0, return NULL, that is, a null pointer.
(1) When n=1, return the empty string "".
(1) If the read is successful, the address of the buffer is returned.
(1) If there is a read error or end-of-file (EOF) is encountered, NULL is returned.
*Note: fgets() is different from gets(), not only because the gets() function has only one parameter FILE *stream, but more importantly, fgets() can specify the maximum read string number, eliminating the problem of buffer overflow caused by improper use of gets()
Recommended tutorial: "C Language"
The above is the detailed content of What is the usage of fgets function in C language?. For more information, please follow other related articles on the PHP Chinese website!