This chapter delves into the world of binary file input and output (I/O) in C++. Binary files store data in its raw form, unlike text files which use human-readable characters. Understanding binary I/O allows you to work with various data types, images, or other non-textual information using C++.
\n
) mark the end of lines.C++ provides file stream classes for interacting with files:
ofstream
: Output file stream for writing data to a file.ifstream
: Input file stream for reading data from a file.fstream
: Can be used for both reading and writing to a file.When opening a file for binary I/O, you need to specify the ios::binary
mode flag:
std::ofstream outfile("data.bin", std::ios::binary);
std::ifstream infile("data.bin", std::ios::binary);
Use the stream insertion operator (<<
) to write data to a binary file:
int number = 42;
double pi = 3.14159;
outfile << number << pi; // Write data to the file
Use the stream extraction operator (>>
) to read data from a binary file:
int number;
double pi;
infile >> number >> pi; // Read data from the file
The order of data written to the file must match the order you read it back. Binary files don’t inherently understand data types. You are responsible for keeping track of the data types and their sizes when reading and writing.
#include
#include
int main() {
// Write integers to a binary file
std::ofstream outfile("data.bin", std::ios::binary);
int num1 = 100;
int num2 = 200;
outfile << num1 << num2;
outfile.close();
// Read integers from the binary file
std::ifstream infile("data.bin", std::ios::binary);
int result1, result2;
infile >> result1 >> result2;
infile.close();
std::cout << "Read values from file: " << result1 << ", " << result2 << std::endl;
return 0;
}
num1
and num2
) to the binary file “data.bin”.result1
and result2
) from the same file.Note: This is a basic example. Real-world scenarios might involve more complex data structures or error handling.
#include
#include
union Data {
int value;
char bytes[sizeof(int)];
};
int main() {
// Write an integer to a binary file on the current system's endianness
Data data;
data.value = 123456;
std::ofstream outfile("data.bin", std::ios::binary);
outfile.write(data.bytes, sizeof(data.bytes));
outfile.close();
// Read the integer from the binary file (assuming same endianness)
Data readData;
std::ifstream infile("data.bin", std::ios::binary);
infile.read(readData.bytes, sizeof(readData.bytes));
infile.close();
std::cout << "Read value from file: " << readData.value << std::endl;
return 0;
}
union
named Data
is used to group an integer (value
) and an array of characters (bytes
) with the same size as the integer.data.value
member, and the union automatically stores the value in memory according to the system’s endianness.bytes
array of the data
union is written to the binary file.bytes
array is read back from the file, and the value
member of the readData
union will interpret the bytes according to the same endianness as when it was written.Note: This example assumes the same endianness when writing and reading. In real-world scenarios, you might need to implement byte swapping techniques if you plan to read binary files written on a different architecture with a different endianness.
ios::binary
mode when opening files for binary I/O.By following these guidelines, you can leverage binary file I/O to create versatile C++ applications that handle diverse data types and file operations.
Binary file I/O provides a powerful mechanism for working with various data types and non-textual information in C++. By understanding the concepts, following best practices, and considering advanced topics like endianness, you can effectively store, retrieve, and manipulate data using binary files in your C++ programs. Happy coding !❤️