fgets() in C

"fgets()" is a standard library function in C Language used for reading a line of text from a file or from the standard input (keyboard).

It reads characters from the specified input stream up to and including a newline character ('\n') or until the specified number of characters is read.

`fgets()` returns a pointer to the read string if successful, or NULL if an error occurs or if the end-of-file (EOF) is reached before any characters are read.

Features of fgets() in C:

  • Line Input: It enables us to read entire lines of text from files or input streams, making it suitable for processing text-based data.

  • Buffer Management: It automatically appends a null character ('\0') to the end of the string it reads, making it safe to use with C strings.

Syntax:

char *fgets(char *str, int n, FILE *stream);
  • "str": Pointer to a character array where the line of text will be stored.

  • "n": Maximum number of characters to read, including the null terminator ('\0').

  • "stream": Pointer to a "FILE" object that identifies the input stream.

The "fgets()" function returns str if successful, or "NULL" if an error occurs or if the end of the file is reached before any characters are read.

Reading Lines from Standard Input (Keyboard):

Reads lines of text from the standard input (keyboard) and prints them back:

Example:

#include <stdio.h>

int main() {
    char line[100]; // Assuming lines are no longer than 99 characters

    printf("Enter lines of text (Ctrl+D to end):\n");
    
    // Read lines from standard input until Ctrl+D (EOF) is encountered
    while (fgets(line, sizeof(line), stdin) != NULL) {
        // Print the line read
        printf("You entered: %s", line);
    }

    return 0;
}

Reading Lines from a File:

Example:

#include <stdio.h>

int main() {
    char line[100]; // Assuming lines are no longer than 99 characters

    printf("Enter lines of text (Ctrl+D to end):\n");
    
    // Read lines from standard input until Ctrl+D (EOF) is encountered
    while (fgets(line, sizeof(line), stdin) != NULL) {
        // Print the line read
        printf("You entered: %s", line);
    }

    return 0;
}

Reads lines of text from a file and prints them:

Example:

#include <stdio.h>
#include <string.h>

int main() {
    FILE *file;
    char line[100]; // Assuming lines are no longer than 99 characters
    char *token;

    // Open the file in read mode
    file = fopen("example.txt", "r");
    
    // Check if file opened successfully
    if (file == NULL) {
        perror("Error opening file");
        return 1;
    }

    // Read lines from the file until EOF is encountered
    while (fgets(line, sizeof(line), file) != NULL) {
        // Tokenize the line by whitespace and process each token
        token = strtok(line, " \t\n");
        while (token != NULL) {
            // Print each token
            printf("Token: %s\n", token);
            // Get the next token
            token = strtok(NULL, " \t\n");
        }
    }

    // Close the file
    fclose(file);

    return 0;
}

Processing Each Line:

Process each line read using "fgets()".

Note:

"fgets()" can be used to read lines of text from standard input or files, allowing for processing and manipulation of the text data.