File Handling in C

Class12 NEB -Computer Science Exam for Computer Science

Posted by yanib on 2025-03-16 08:35:57 | Last Updated by yanib on 2025-07-19 01:34:30

Share: Facebook | Twitter | Whatsapp | Linkedin Visits: 224


File Handling in C

File Handling in C

File Handling in C refers to the process of creating, reading, writing, and closing files from within a C program. Files are used to store data permanently (as opposed to variables that exist only while the program is running). In C, file handling is done using the standard library functions defined in the <stdio.h> header file. Through file handling, you can interact with files on the computer’s storage, perform operations like writing data, reading data, and managing file content.

Modes of File Handling in C

When opening a file in C using the fopen() function, you specify a mode to indicate the type of operation you intend to perform on the file. Here are the common modes used in C for file handling:

1. "r" - Read Mode

  • Description: Opens the file for reading.
  • Use case: If the file exists, you can read its contents. If the file doesn’t exist, the fopen() call will return NULL.
  • Example:
    FILE *file = fopen("example.txt", "r");

2. "w" - Write Mode

  • Description: Opens the file for writing.
  • Use case: If the file exists, it will be overwritten. If the file doesn’t exist, a new file will be created.
  • Example:
    FILE *file = fopen("example.txt", "w");

3. "a" - Append Mode

  • Description: Opens the file for writing, but it appends data at the end of the file.
  • Use case: If the file exists, data will be added at the end without overwriting the existing content. If the file doesn’t exist, it will be created.
  • Example:
    FILE *file = fopen("example.txt", "a");

4. "r+" - Read/Write Mode

  • Description: Opens the file for both reading and writing.
  • Use case: The file must exist. If the file doesn’t exist, fopen() will return NULL. This mode allows you to read from and write to the file.
  • Example:

    FILE *file = fopen("example.txt", "r+");

5. "w+" - Write/Read Mode

  • Description: Opens the file for both writing and reading.
  • Use case: If the file exists, it will be overwritten. If the file doesn’t exist, a new file is created. The file can be both read and written to.
  • Example:
    FILE *file = fopen("example.txt", "w+");

6. "a+" - Append/Read Mode

  • Description: Opens the file for both reading and appending.
  • Use case: If the file exists, new data will be added at the end of the file. If the file doesn’t exist, it will be created. This mode allows both reading and appending.
  • Example:
    FILE *file = fopen("example.txt", "a+");

7. "b" - Binary Mode

  • Description: This is an optional mode that can be added to any of the above modes to handle binary files.
  • Use case: This mode is used when you need to work with binary files, such as images or audio files.
  • Example:
    • To open a binary file for reading:
      FILE *file = fopen("example.bin", "rb");
    • To open a binary file for writing:
      FILE *file = fopen("example.bin", "wb");

Summary of Modes

ModeDescription
"r"Opens the file for reading. The file must exist.
"w"Opens the file for writing. If the file exists, it is overwritten.
"a"Opens the file for appending. If the file exists, new data is added at the end.
"r+"Opens the file for reading and writing. The file must exist.
"w+"Opens the file for writing and reading. The file is overwritten if it exists.
"a+"Opens the file for reading and appending. Data is added at the end.
"rb", "wb", "ab"For binary files (read, write, append in binary mode).

Example of using fopen() with modes:

#include <stdio.h>
int main() { FILE *file; // Open a file in write mode file = fopen("example.txt", "w"); if (file == NULL) { printf("Unable to open file. "); return 1; } fprintf(file, "Hello, World! "); fclose(file); // Open the same file in read mode file = fopen("example.txt", "r"); if (file == NULL) { printf("Unable to open file. "); return 1; } char ch; while ((ch = fgetc(file)) != EOF) { putchar(ch); } fclose(file); return 0; }

This example opens a file for writing, writes to it, and then opens it for reading and prints the contents.

Leave a Comment: