Text File vs Binary File in C: Key Differences and Usage
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.
| Aspect | Text File | Binary File |
|---|---|---|
| Data Storage | Stores data as readable characters | Stores data as raw bytes |
| File Modes | "r", "w", "a" (text modes) | "rb", "wb", "ab" (binary modes) |
| Newline Handling | Translates newline characters (e.g., '\n' to platform-specific) | No translation; bytes are read/written as-is |
| EOF Handling | EOF detected by special character or condition | EOF detected by file size |
| Use Case | For human-readable data like text | For non-text data like images, executables |
| Portability | More portable across platforms | Less 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.
#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; }
Binary File Equivalent
This example writes and reads the same string as raw bytes in a binary file.
#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; }
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.