Home  >  Article  >  Backend Development  >  What is the usage of fgets function in C language?

What is the usage of fgets function in C language?

烟雨青岚
烟雨青岚Original
2020-06-11 13:40:5914959browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn