Skip to content

Introduction to File Handling in C

File handling in C involves operations related to reading from and writing to files. It allows you to interact with files on the system, enabling tasks such as creating, opening, reading, writing, and closing files. Here’s an introduction to file handling in C:

1. Opening and Closing Files:

  • Before performing any operations on a file, it must be opened using the fopen() function, which returns a pointer to a FILE structure.
  • After completing operations on the file, it should be closed using the fclose() function to release the resources associated with it.

2. Reading from Files:

  • File reading operations involve reading data from a file and storing it in variables or arrays in memory.
  • The fscanf() and fgets() functions are commonly used to read data from files. fscanf() is used for formatted input, while fgets() is used for reading lines of text.

3. Writing to Files:

  • File writing operations involve writing data from variables or arrays in memory to a file.
  • The fprintf() and fputs() functions are commonly used to write data to files. fprintf() is used for formatted output, while fputs() is used for writing strings.

4. Error Handling:

  • Error handling is crucial when performing file operations to handle situations such as file not found, insufficient permissions, or disk full errors.
  • Functions like feof() and ferror() are used to check for end-of-file and error conditions during file operations.

5. File Modes:

  • When opening a file, you specify a mode that determines the type of operations allowed on the file.
  • Common file modes include “r” (read), “w” (write), “a” (append), “rb” (read in binary mode), and “wb” (write in binary mode).

6. File Pointers:

  • File pointers are used to keep track of the current position within a file during read and write operations.
  • They automatically advance after each read or write operation, allowing sequential access to file data.

7. Binary vs. Text Files:

  • Files can be classified into binary and text files based on the format of data they contain.
  • Text files contain human-readable characters, while binary files contain raw data in binary format.
  • Different file handling functions are used for text and binary file operations.

Example: Reading from a Text File:

#include <stdio.h>

int main()

{

    FILE *file;

    char buffer[100];

    file = fopen(“example.txt”, “r”);  // Open file in read mode

    if (file == NULL) {

        perror(“Error opening file”);

        return 1;

    }

    // Read and print each line from the file

    while (fgets(buffer, sizeof(buffer), file) != NULL) {

        printf(“%s”, buffer);

    }

    fclose(file);  // Close the file

    return 0;

} In this example, the program opens a file named “example.txt” in read mode using fopen(), reads each line from the file using fgets(), prints the lines to the console, and finally closes the file using fclose(). Proper error handling is implemented to check if the file was opened successfully.