Text and binary file modes
Lesson 7.2: Text and Binary File Modes
In C, when you open a file using fopen(), you need to specify a mode indicating how you intend to access the file. The two most fundamental modes are text mode and binary mode. Understanding the difference is crucial for handling file data correctly.
Text Mode (Mode Character: "t")
Human-Readable Characters: Text files are designed to store characters that are readable by humans (e.g., letters, numbers, punctuation, and symbols).
Newline Conversion:
In text mode, C performs automatic newline character conversion to match the operating system's conventions.
On Windows, a newline is represented by two characters (carriage return \r followed by line feed \n). In text mode, when you write a single \n, the C library converts it to \r\n. When you read, it converts \r\n back to \n.
On Unix-like systems (Linux, macOS), a newline is just \n.
Special Character Handling: Some characters might be interpreted differently in text mode (e.g., Control-Z on Windows can signal the end of a file).
Binary Mode (Mode Character: "b")
Raw Data: Binary files are designed to store any type of data, including non-textual data like images, music, or executable code.
No Conversion: In binary mode, no automatic conversions are performed on the data. Each byte is read or written exactly as it appears in the file.
Preserves Data Integrity: Binary mode is essential when you need to preserve the exact byte values in your data.
Choosing the Right Mode
File Type Mode Explanation
Text files (e.g., .txt, .csv, .c) "r"/"w"/"a" Use these for text files when you want newline conversion to be handled automatically.
Binary files (e.g., .exe, .jpg, .mp3) "rb"/"wb"/"ab" Use these for binary files where you want the exact bytes to be read or written without any modifications.
Text files (reading and writing) "r+"/"w+"/"a+" These modes allow both reading and writing, but be cautious with positioning the file pointer when switching between operations.
Note: On Unix-like systems, there is no actual difference between text and binary mode at the file system level. However, it's still good practice to use the correct mode to indicate your intent.
Example: Writing and Reading Binary Data
C
#include <stdio.h>
#include <stdlib.h>
int main() {
// Write binary data
FILE *outfile = fopen("binarydata.bin", "wb");
if (outfile == NULL) { /* Error handling */ }
double data[] = {3.14159, 2.71828};
fwrite(data, sizeof(double), 2, outfile);
fclose(outfile);
// Read binary data
FILE *infile = fopen("binarydata.bin", "rb");
if (infile == NULL) { /* Error handling */ }
double readData[2];
fread(readData, sizeof(double), 2, infile);
fclose(infile);
// Use the read data
printf("%.5f %.5f\n", readData[0], readData[1]);
return 0;
}
In this example, we write two double values to a binary file and then read them back, demonstrating how binary mode preserves the exact values.
Let me know if you'd like any clarification or more examples on this topic!