top of page

File Handling

  • Writer: Siddharth Sharma
    Siddharth Sharma
  • Dec 19, 2024
  • 2 min read

File handling in programming refers to the process of creating, reading, writing, and managing files. It allows data to be stored and retrieved from secondary storage (e.g., hard drives), enabling persistent storage beyond the program's execution.


Structure of File Handling

File handling typically involves the following steps:

  1. Open the File: Specify the file name and mode (e.g., read, write, append).

  2. Perform Operations: Read from or write to the file as needed.

  3. Close the File: Release resources associated with the file.


File Types

  1. Text Files: Store data in a human-readable format, typically as plain text.

  2. Binary Files: Store data in a non-human-readable format, allowing more efficient storage and retrieval.


File Handling Functions (C Language)

In C, file handling is managed using the <stdio.h> library, which provides the following functions:

  1. fopen()

    • Opens a file in the specified mode (e.g., read, write, append).

    • Returns a FILE * pointer.

    Example:

FILE *fp = fopen("example.txt", "r");
  1. fclose()

    • Closes an opened file.

    Example:

fclose(fp);
  1. fprintf() and fscanf()

    • Used for writing to and reading from text files, respectively.

    Example:

fprintf(fp, "Hello, World!\n"); fscanf(fp, "%d", &value);
  1. fwrite() and fread()

    • Used for writing to and reading from binary files.

    Example:

fwrite(&data, sizeof(data), 1, fp); fread(&data, sizeof(data), 1, fp);
  1. fseek() and ftell()

    • Used for navigating within a file.

    Example:

fseek(fp, 0, SEEK_END); long size = ftell(fp);
  1. feof() and ferror()

    • Used for detecting the end of a file and errors, respectively.

    Example:

if (feof(fp)) { printf("End of file reached.\n"); } if (ferror(fp)) { printf("Error occurred.\n"); }

Buffered vs. Unbuffered Files

  1. Buffered Files: Use an intermediate buffer to improve I/O performance by reducing direct disk access.

    • Example: Standard file handling functions like fopen() are buffered.

  2. Unbuffered Files: Do not use intermediate buffers, directly interacting with the disk.

    • Example: Low-level system calls like open().


Error Handling in File Operations

Error handling ensures the program gracefully manages issues such as missing files or write failures. Common techniques include:

  1. Checking the return value of file functions (e.g., fopen() returns NULL on failure).

  2. Using perror() to print error messages.


Example:

FILE *fp = fopen("nonexistent.txt", "r");
if (fp == NULL) {
    perror("Error opening file");
    return 1;
}

Example: File Handling in C

#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *fp;
    char filename[] = "example.txt";
    char data[100];

    // Open file for writing
    fp = fopen(filename, "w");
    if (fp == NULL) {
        perror("Error opening file");
        return 1;
    }
    fprintf(fp, "Hello, File Handling!\n");
    fclose(fp);

    // Open file for reading
    fp = fopen(filename, "r");
    if (fp == NULL) {
        perror("Error opening file");
        return 1;
    }
    while (fgets(data, sizeof(data), fp) != NULL) {
        printf("%s", data);
    }
    fclose(fp);

    return 0;
}

This example demonstrates basic file operations, including writing to and reading from a text file. Always ensure files are properly closed to release resources.


 
 
 

Comments


bottom of page