8.2) Text files and Binary files in C++
In C++, files can be categorized into two main types: text files and binary files. The distinction between these two types lies in how the data is stored and processed in the files.
Table of Contents
Text Files
Text files store data as human-readable characters, using character encodings like ASCII or UTF-8. Text files are often used for storing plain text, such as configuration files, code files, and documents.
Here’s an example of reading and writing to a text file:
#include <iostream>
#include <fstream>
int main() {
// Writing to a text file
std::ofstream outputFile("textfile.txt"); // Open for writing
if (outputFile.is_open()) {
outputFile << "Hello, Text File!" << std::endl;
outputFile << "Line 2" << std::endl;
outputFile.close(); // Close the file
std::cout << "Data written to text file." << std::endl;
} else {
std::cerr << "Failed to open the file for writing." << std::endl;
}
// Reading from a text file
std::ifstream inputFile("textfile.txt"); // Open for reading
if (inputFile.is_open()) {
std::string line;
while (getline(inputFile, line)) {
std::cout << "Read from text file: " << line << std::endl;
}
inputFile.close(); // Close the file
} else {
std::cerr << "Failed to open the file for reading." << std::endl;
}
return 0;
}
Output:
Data written to text file.
Read from text file: Hello, Text File!
Read from text file: Line 2
Binary Files
Binary files store data in its raw binary form, without any character encoding. Binary files are used for storing non-textual data like images, audio files, and compiled programs.
Here’s an example of reading and writing to a binary file:
#include <iostream>
#include <fstream>
int main() {
// Writing to a binary file
std::ofstream outputFile("binaryfile.bin", std::ios::binary); // Open for writing in binary mode
if (outputFile.is_open()) {
int numbers[] = {1, 2, 3, 4, 5};
outputFile.write(reinterpret_cast<char*>(numbers), sizeof(numbers));
outputFile.close(); // Close the file
std::cout << "Data written to binary file." << std::endl;
} else {
std::cerr << "Failed to open the file for writing." << std::endl;
}
// Reading from a binary file
std::ifstream inputFile("binaryfile.bin", std::ios::binary); // Open for reading in binary mode
if (inputFile.is_open()) {
int numbers[5];
inputFile.read(reinterpret_cast<char*>(numbers), sizeof(numbers));
inputFile.close(); // Close the file
std::cout << "Read from binary file: ";
for (int num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
} else {
std::cerr << "Failed to open the file for reading." << std::endl;
}
return 0;
}
Output:
Data written to binary file.
Read from binary file: 1 2 3 4 5
In the binary file example, the data is stored in its raw binary format. Notice that we use the std::ios::binary
flag when opening the file to indicate binary mode. Additionally, the reinterpret_cast
is used to convert between pointers to different types when reading/writing binary data.
Remember to choose the appropriate type of file (text or binary) based on the nature of the data you want to store, and ensure you use the correct mode when opening files.