C - Advanced File I/O and Binary I/O in C

In C programming, file handling allows a program to store data permanently in files instead of keeping it only in memory. Basic file handling usually covers opening, reading, writing, and closing text files. However, advanced file I/O goes deeper by working with binary files, handling errors properly, and managing files efficiently in real-world applications.

1. Text Files vs Binary Files

A text file stores data in a human-readable format. For example, numbers and characters are stored as characters. When you write the number 123 to a text file, it is stored as three characters: ‘1’, ‘2’, and ‘3’.

A binary file stores data in the same format as it is stored in memory. For example, an integer is written as 4 bytes (or depending on system size), not as characters. Binary files are not human-readable but are more efficient for storing structured data such as records.

Text file functions commonly used:

  • fprintf()

  • fscanf()

  • fgets()

  • fputs()

Binary file functions:

  • fread()

  • fwrite()

2. Working with Binary Files

To work with binary files, you must open the file in binary mode:

  • "rb" – Read binary

  • "wb" – Write binary

  • "ab" – Append binary

Example of writing a structure to a binary file:

#include <stdio.h>

struct Student {
    int id;
    float marks;
};

int main() {
    FILE *fp;
    struct Student s = {1, 85.5};

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

    return 0;
}

Here:

  • &s is the address of the structure.

  • sizeof(s) tells how many bytes to write.

  • 1 means writing one record.

To read the same data:

fread(&s, sizeof(s), 1, fp);

Binary files are commonly used in databases, game development, and embedded systems because they are faster and take less space.

3. File Positioning Functions

Advanced file handling includes moving the file pointer manually using:

  • fseek() – Moves file pointer to a specific location.

  • ftell() – Returns current file pointer position.

  • rewind() – Moves pointer to beginning of file.

Example:

fseek(fp, 0, SEEK_END);
long size = ftell(fp);
rewind(fp);

These functions are useful when:

  • Calculating file size

  • Accessing specific records

  • Updating particular parts of a file

4. Error Handling in File I/O

In real programs, file operations can fail. Always check if the file opened successfully:

fp = fopen("data.txt", "r");
if (fp == NULL) {
    printf("Error opening file");
    return 1;
}

You can also use:

  • feof() – Checks end of file

  • ferror() – Checks for file errors

  • perror() – Prints system error message

Proper error handling makes programs reliable and prevents crashes.

5. Low-Level File Handling

Apart from standard file functions, C also supports low-level file operations using system calls like:

  • open()

  • read()

  • write()

  • close()

These are used in system programming and give more control over file handling but are platform-dependent.

6. Why Advanced File I/O is Important

Advanced file handling is important because:

  • It allows efficient storage of structured data.

  • It improves performance in large applications.

  • It helps build real-world systems like banking software, student management systems, and embedded applications.

  • It enables direct memory-like access to stored records.

In summary, advanced file I/O in C goes beyond simple reading and writing. It focuses on binary storage, file positioning, error handling, and efficient data management, which are essential for professional C programming.