fgetc() Function

The fgetc() function in C reads a single character from an input stream and advances the internal file position indicator. It is often used for reading files character by character, detecting the end-of-file, or checking for read errors.


Syntax of fgetc()

</>
Copy
int fgetc(FILE *stream);

Parameters

ParameterDescription
streamA pointer to a FILE object that identifies the input stream to read from.

It is important to note that fgetc() returns the character read (promoted to an int) on success. When the end-of-file is reached or an error occurs, it returns EOF and sets the appropriate indicator (feof or ferror).

Return Value

On success, the character read is returned (as an int). If the end-of-file is reached, or if a read error occurs, fgetc() returns EOF and sets the corresponding indicator for the stream.


Examples for fgetc()

Example 1: Reading a Single Character from a File

This example demonstrates how to open a file, read a single character using fgetc(), and then close the file.

Program

</>
Copy
#include <stdio.h>

int main() {
    FILE *fp = fopen("sample.txt", "r");
    if (fp == NULL) {
        perror("Error opening file");
        return 1;
    }
    
    int ch = fgetc(fp);
    if(ch != EOF) {
        printf("First character: %c\n", ch);
    } else {
        printf("No character read or error occurred.\n");
    }
    
    fclose(fp);
    return 0;
}

Explanation:

  1. The file "sample.txt" is opened in read mode.
  2. fgetc() is used to read the first character from the file.
  3. If a character is successfully read (i.e., not EOF), it is printed.
  4. The file is then closed to free up resources.

Program Output:

First character: H

Example 2: Reading a File Character by Character Until End-of-File

This example illustrates how to use fgetc() in a loop to read all characters from a file until the end-of-file is reached.

Program

</>
Copy
#include <stdio.h>

int main() {
    FILE *fp = fopen("sample.txt", "r");
    if (fp == NULL) {
        perror("Error opening file");
        return 1;
    }
    
    int ch;
    while ((ch = fgetc(fp)) != EOF) {
        putchar(ch);
    }
    
    fclose(fp);
    return 0;
}

Explanation:

  1. The file "sample.txt" is opened in read mode.
  2. A loop is used to read each character using fgetc() until EOF is encountered.
  3. Each character read is printed immediately using putchar().
  4. The file is closed after reading is complete.

Program Output:

Contents of sample.txt are printed here exactly as they appear.

Example 3: Handling End-of-File and Read Errors

This example demonstrates how to check for the end-of-file and handle potential read errors when using fgetc().

Program

</>
Copy
#include <stdio.h>

int main() {
    FILE *fp = fopen("sample.txt", "r");
    if (fp == NULL) {
        perror("Error opening file");
        return 1;
    }
    
    int ch;
    while (1) {
        ch = fgetc(fp);
        if (ch == EOF) {
            if (feof(fp)) {
                printf("\nEnd of file reached.\n");
            }
            if (ferror(fp)) {
                perror("\nError reading file");
            }
            break;
        }
        putchar(ch);
    }
    
    fclose(fp);
    return 0;
}

Explanation:

  1. The file "sample.txt" is opened for reading.
  2. A loop continuously reads characters using fgetc().
  3. When EOF is returned, the program checks if it is due to reaching the end-of-file or a read error.
  4. The appropriate message is printed based on the condition.
  5. The file is closed once the reading loop terminates.

Program Output:

(File contents printed, followed by:
End of file reached.)