C Programming

⌘K
  1. Home
  2. Docs
  3. C Programming
  4. File Handling in C
  5. Modes of File

Modes of File

Learn all modes of file in C programming — including read, write, append, and binary modes. Understand how fopen() works with examples and file handling operations.


Introduction: What are File Modes in C Programming?

In C programming, file modes define how a file is opened and what operations can be performed on it — such as reading, writing, or appending data.

When you open a file using the fopen() function, you must specify the mode to tell the compiler what kind of operation you intend to perform.

File modes are crucial for efficient data storage, retrieval, and manipulation in both text files and binary files.


Syntax of fopen() Function

FILE *fp;
fp = fopen("filename.txt", "mode");
  • filename.txt → Name of the file
  • mode → Mode in which the file is opened
  • fp → File pointer returned by fopen()

If fopen() fails (e.g., file doesn’t exist), it returns NULL.


Types of File Modes in C

There are two categories of file modes in C programming:

  1. Text File Modes
  2. Binary File Modes

Let’s explore each type in detail.


1️⃣ Text File Modes

Text file modes are used to handle human-readable data (ASCII format).
They are most commonly used for reading and writing simple text files.

ModeDescription
"r"Opens an existing file for reading only. File must exist.
"w"Opens a file for writing. If file exists, previous content is overwritten.
"a"Opens a file for appending new data at the end. Creates file if not found.
"r+"Opens a file for both reading and writing. File must exist.
"w+"Opens a file for reading and writing. If file exists, content is overwritten.
"a+"Opens a file for reading and appending. New data is added at the end.

Example: Using Text File Modes

#include <stdio.h>

int main() {
    FILE *fp;

    // Write mode example
    fp = fopen("example.txt", "w");
    fprintf(fp, "Hello, File Handling in C!\n");
    fclose(fp);

    // Read mode example
    fp = fopen("example.txt", "r");
    char ch;
    while((ch = fgetc(fp)) != EOF)
        putchar(ch);

    fclose(fp);
    return 0;
}

Explanation:

  • "w" → Writes text into the file (creates if not found).
  • "r" → Reads characters from the file until EOF (End of File).

2️⃣ Binary File Modes

Binary file modes are used to handle machine-readable data.
They are faster, more compact, and ideal for storing structured data, images, or records.

ModeDescription
"rb"Opens a binary file for reading. File must exist.
"wb"Opens a binary file for writing. Overwrites existing content.
"ab"Opens a binary file for appending. Adds data to end of file.
"rb+"Opens a binary file for both reading and writing. File must exist.
"wb+"Opens a binary file for reading and writing. Overwrites old content.
"ab+"Opens a binary file for reading and appending. Data added at end.

Example: Using Binary File Modes

#include <stdio.h>

struct Student {
    char name[30];
    int roll;
    float marks;
};

int main() {
    struct Student s1 = {"Alice", 102, 91.5};
    FILE *fp;

    fp = fopen("student.dat", "wb");
    fwrite(&s1, sizeof(s1), 1, fp);
    fclose(fp);

    printf("Binary data written successfully!");
    return 0;
}

Explanation:

  • "wb" opens file in binary write mode.
  • fwrite() stores raw memory data directly to the file.

Key Differences Between Text and Binary File Modes

FeatureText File ModeBinary File Mode
Data TypeASCII / Human-readableMachine-readable (binary)
SpeedSlowerFaster
Storage SizeLargerSmaller
PrecisionMay lose data precisionExact representation
Common UseLogs, reports, configurationsImages, records, programs

Best Practices for Using File Modes in C

✅ Always check if file opened successfully:

if (fp == NULL) {
    printf("Error opening file!");
}

✅ Use fclose(fp); to prevent memory leaks.
✅ Select correct mode based on operation (read/write/append).
✅ Avoid overwriting critical data accidentally with "w" or "wb".
✅ Use "a" or "a+" to safely add data without deleting old content.


Common Errors in File Handling

  • Opening non-existent files in "r" mode.
  • Forgetting to close files.
  • Using wrong mode for binary vs text files.
  • Writing data in binary mode and reading it in text mode.

Conclusion

The modes of file in C programming determine how a file is opened and accessed.
Whether you are reading text data or writing binary records, choosing the right mode — such as "r", "w", "a", "rb", "wb", or "ab" — is essential for correct file handling.

Mastering file modes enables developers to build robust, data-driven applications in C with efficiency and precision.

Tags , ,

How can we help?

Discussion 0

Join the Conversation

Your email address will not be published. Required fields are marked *