0
0
CComparisonBeginner · 4 min read

Text File vs Binary File in C: Key Differences and Usage

In C, a text file stores data as readable characters with special handling for newline and EOF, while a binary file stores data as raw bytes without translation. Text files are opened with modes like "r" or "w", and binary files use modes like "rb" or "wb" to prevent data alteration during reading or writing.
⚖️

Quick Comparison

This table summarizes the main differences between text and binary files in C.

AspectText FileBinary File
Data StorageStores data as readable charactersStores data as raw bytes
File Modes"r", "w", "a" (text modes)"rb", "wb", "ab" (binary modes)
Newline HandlingTranslates newline characters (e.g., '\n' to platform-specific)No translation; bytes are read/written as-is
EOF HandlingEOF detected by special character or conditionEOF detected by file size
Use CaseFor human-readable data like textFor non-text data like images, executables
PortabilityMore portable across platformsLess portable due to raw byte format
⚖️

Key Differences

Text files in C store data as characters that can be read and understood by humans. When you open a text file, the system may translate newline characters to match the platform's format (for example, converting '\n' to '\r\n' on Windows). This makes text files easy to edit with simple text editors.

In contrast, binary files store data exactly as bytes without any translation. This means the data is preserved exactly as it is, which is important for files like images, audio, or compiled programs. When reading or writing binary files, you use modes like "rb" or "wb" to tell the system not to alter the data.

Another key difference is how the end of the file (EOF) is handled. Text files detect EOF by a special character or condition, while binary files rely on the file size. This affects how you read data and check for the end of the file in your program.

⚖️

Code Comparison

This example shows how to write and read a simple string to a text file in C.

c
#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "w");
    if (file == NULL) {
        perror("Failed to open file");
        return 1;
    }
    fprintf(file, "Hello, text file!\n");
    fclose(file);

    char buffer[50];
    file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("Failed to open file");
        return 1;
    }
    if (fgets(buffer, sizeof(buffer), file) != NULL) {
        printf("Read from text file: %s", buffer);
    }
    fclose(file);
    return 0;
}
Output
Read from text file: Hello, text file!
↔️

Binary File Equivalent

This example writes and reads the same string as raw bytes in a binary file.

c
#include <stdio.h>
#include <string.h>

int main() {
    const char *text = "Hello, binary file!";
    FILE *file = fopen("example.bin", "wb");
    if (file == NULL) {
        perror("Failed to open file");
        return 1;
    }
    fwrite(text, sizeof(char), strlen(text), file);
    fclose(file);

    char buffer[50] = {0};
    file = fopen("example.bin", "rb");
    if (file == NULL) {
        perror("Failed to open file");
        return 1;
    }
    size_t bytesRead = fread(buffer, sizeof(char), sizeof(buffer) - 1, file);
    buffer[bytesRead] = '\0';
    printf("Read from binary file: %s\n", buffer);
    fclose(file);
    return 0;
}
Output
Read from binary file: Hello, binary file!
🎯

When to Use Which

Choose text files when you need to store data that humans will read or edit, such as configuration files, logs, or source code. Text files are easier to debug and portable across different systems.

Choose binary files when you need to store data exactly as it is, like images, audio, video, or compiled programs. Binary files are more efficient for storage and speed but are not human-readable and less portable.

Key Takeaways

Text files store readable characters with newline translation; binary files store raw bytes without translation.
Use text mode ("r", "w") for text files and binary mode ("rb", "wb") for binary files in C.
Text files are portable and human-readable; binary files are efficient for non-text data but less portable.
EOF detection differs: text files use special characters or conditions, binary files rely on file size.
Choose file type based on whether data needs to be human-readable or stored exactly as bytes.