Pointer to the buffer where the input string will be stored
2nd Parameter Type : int
Integer type (typically 4 bytes, -2,147,483,648 to 2,147,483,647)
2nd Parameter
The maximum number of characters to read, including the null terminator
3rd Parameter Type : FILE *
Pointer to FILE structure for file operations
3rd Parameter
Pointer to the file stream from which the input is read
Read more about parameters of fgets in parameters section
The fgetsfunction in C language reads a line from the specified stream and stores it into the string pointed to by str, up to the user specified number of characters.
fgets is a secure line-reading function in C, designed specifically for safely reading strings with a buffer size limit to prevent overflow. Unlike gets (which is now deprecated due to security issues), fgets requires explicit buffer size specification and preserves newline characters. It is commonly used in conjunction with file I/O functions like fopen for reading files line by line, and error-checking functions like ferror and feof to handle read failures. The function is particularly useful in text processing applications where line-by-line reading is needed, and it's often preferred over character-by-character reading functions like getc when dealing with text files. This function is special because: • It handles buffer boundaries safely; • It preserves newline characters (unlike some other string functions); • It works with any stream (not just stdin); • It's part of the standard C library's safer string handling functions;
To Summarize the Workflow:
1.
Function reads a line from specified stream into given string buffer
2.
Takes three parameters: destination buffer (str), max characters to read (n), and input stream
3.
Reads until: newline character found, n-1 characters read, or EOF reached
4.
Adds null terminator after last character read
5.
Returns str pointer on success, NULL on EOF or error
6.
Includes newline character in output string if found
Read more about return type and value offgets function in return section.
The fgetsfunction takes 3
parameters:
•
char * `str`: Pointer to the buffer where the input string will be stored
•
int `n`: The maximum number of characters to read, including the null terminator
•
FILE * `stream`: Pointer to the file stream from which the input is read
Reads at most `n-1` characters from the specified file stream and stores them in the buffer pointed to by `str`. The function stops reading on encountering a newline character or the end-of-file. The resulting string is null-terminated.
The fgets function return value :
On success, the function returns str;
If the end-of-file is encountered while attempting to read a character, the EOF indicator is set (feof);
If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged);
If a read error occurs, the error indicator (ferror) is set and a null pointer is also returned;
Output
This example demonstrates the basic usage of `fgets`. It reads a file line by line and prints each line to the console.