Explain read mode operation of files in C language

In C programming, file handling allows us to perform various operations on files stored on disk. The read mode operation is one of the fundamental file operations that enables us to access and retrieve data from existing files.

Syntax

FILE *file_pointer;
file_pointer = fopen("filename", "r");

File Declaration and Opening in Read Mode

To work with files in C, we first need to declare a file pointer and then open the file in read mode −

FILE *fp;
fp = fopen("filename.txt", "r");

The fopen() function returns a pointer to the file if it exists, or NULL if the file cannot be opened or does not exist.

File Reading Functions

C provides several functions to read data from files −

  • fgetc() − Reads a single character from file
  • fgets() − Reads a string from file
  • fscanf() − Reads formatted data from file
  • fread() − Reads binary data from file

Example 1: Reading Character by Character

The following program demonstrates how to read a file character by character and count the number of characters −

#include <stdio.h>

int main() {
    FILE *fp;
    char ch;
    int charCount = 0;
    
    /* Create a simple text file for demonstration */
    fp = fopen("sample.txt", "w");
    if (fp != NULL) {
        fprintf(fp, "Hello World!\nWelcome to C programming.\nFile handling example.");
        fclose(fp);
    }
    
    /* Open file in read mode */
    fp = fopen("sample.txt", "r");
    if (fp == NULL) {
        printf("Error: Could not open file!<br>");
        return -1;
    }
    
    /* Read character by character */
    printf("File contents:<br>");
    while ((ch = fgetc(fp)) != EOF) {
        printf("%c", ch);
        charCount++;
    }
    
    fclose(fp);
    printf("<br>\nTotal characters: %d<br>", charCount);
    return 0;
}
File contents:
Hello World!
Welcome to C programming.
File handling example.

Total characters: 58

Example 2: Reading Line by Line

This example shows how to read a file line by line using fgets() function −

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

int main() {
    FILE *fp;
    char line[100];
    int lineCount = 0;
    
    /* Create a sample file */
    fp = fopen("data.txt", "w");
    if (fp != NULL) {
        fprintf(fp, "Line 1: Introduction<br>");
        fprintf(fp, "Line 2: File Operations<br>");
        fprintf(fp, "Line 3: Read Mode<br>");
        fprintf(fp, "Line 4: Conclusion<br>");
        fclose(fp);
    }
    
    /* Open file in read mode */
    fp = fopen("data.txt", "r");
    if (fp == NULL) {
        printf("Error opening file!<br>");
        return -1;
    }
    
    /* Read line by line */
    printf("Reading file line by line:<br>");
    while (fgets(line, sizeof(line), fp) != NULL) {
        lineCount++;
        printf("Line %d: %s", lineCount, line);
    }
    
    fclose(fp);
    printf("\nTotal lines read: %d<br>", lineCount);
    return 0;
}
Reading file line by line:
Line 1: Line 1: Introduction
Line 2: Line 2: File Operations
Line 3: Line 3: Read Mode
Line 4: Line 4: Conclusion

Total lines read: 4

Key Points

  • Always check if fopen() returns NULL before performing file operations
  • Use fclose() to properly close the file after operations
  • The file must exist before opening in read mode
  • EOF (End of File) indicates the end of file content
  • Read mode "r" opens file for reading only

Conclusion

File read operations in C provide efficient methods to access stored data. Always ensure proper error checking and file closure to prevent resource leaks and handle errors gracefully.

Updated on: 2026-03-15T13:52:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements