![The C Standard Library Reference](/c_standard_library/images/c-mini-logo.jpg)
- The C Standard Library
- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Standard Library Resources
- C Library - Quick Guide
- C Library - Useful Resources
- C Library - Discussion
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C Library - fgets() function
The C library fgets(FILE *stream) function gets the next character ( unsigned char) from the specified stream and advances the position indicator for the stream.It is commonly used for reading input from a file or from standard input (stdin).
Syntax
Following is the C library syntax of the fgets() function −
char *fgets(char *str, int n, FILE *stream);
Parameters
This function accepts the three parameters −
- char *str :A pointer to an array of characters where the read string will be stored. This array should be large enough to hold the string, including the terminating null character.
- int n: The maximum number of characters to read, including the terminating null character. fgets will read up to n-1 characters, leaving room for the null character.
- FILE *stream: A pointer to a FILE object that specifies the input stream to read from. This can be a file pointer obtained from functions like fopen, or it can be stdin for standard input.
Return Value
On success, fgets returns the same pointer str that was passed in, which now contains the string that was read. If an error occurs, or if end-of-file is reached and no characters were read, fgets returns NULL.
Example 1: Reading a Line from Standard Input
This example reads a line of text from standard input and prints it. The buffer size is 50, allowing for lines up to 49 characters long (plus the null terminator).
Below is the illustration of C library fgets() function.
#include <stdio.h> int main() { char buffer[50]; printf("Enter a string: "); if (fgets(buffer, sizeof(buffer), stdin) != NULL) { printf("You entered: %s", buffer); } else { printf("Error reading input."); } return 0; }
Output
The above code produces following result−
Enter a string: Welcome to tutorials point You entered: Welcome to tutorials point
Example 2: Handling End-of-File
This example reads a file line by line with a smaller buffer size, handling the end-of-file condition explicitly. If the end of the file is reached, a message is printed.
#include <stdio.h> int main() { FILE *file = fopen("example.txt", "r"); char buffer[20]; if (file == NULL) { printf("Failed to open file.\n"); return 1; } while (fgets(buffer, sizeof(buffer), file) != NULL) { printf("Read: %s", buffer); } if (feof(file)) { printf("End of file reached.\n"); } else if (ferror(file)) { printf("An error occurred.\n"); } fclose(file); return 0; }
Output
After execution of above code, we get the following result −
(This output will depend on the contents of example.txt) End of file reached.