Table of Contents
- Opening a File in Binary Mode Using fstream Library
- Writing the Binary File Using the write() Function of fstream Library
- Writing the Binary File Using the put() Function of fstream Library
- Writing the Binary File Using Insertion Operator (<<) Of fstream Library
- Writing the Structure Object to Binary File Using fstream Library
- Conclusion
In this post, we will see how to write binary files in java.
The binary files are similar to the text files however the content in binary files is stored in binary format rather than simple text format.
The binary files can come in handy for storing complex objects such as structures, and classes.
This article discusses methods to write simple data such as string and complex objects such as structure objects to binary files in C++.
Opening a File in Binary Mode Using fstream Library
The fstream
library in C++ provides all sorts of file handling operations. The open()
method of the library is used to open the files in C++.
Let us see the definition of the open()
method.
1 2 3 4 |
void open (const char* filename, ios_base::openmode mode = ios_base::in | ios_base::out); |
The open()
method accepts,
- Path of the file as a character array.
- Mode to open the file.
The mode defines the operations that can be performed on the files. To open the binary file, you must include the ios_base::binary
flag in concatenation to other mode flag variables.
You can read about all mode flags here.
Writing the Binary File Using the write() Function of fstream Library
The write()
function in the fstream
library writes the content of a stream to the file. The definition of the function is given below.
1 2 3 |
ostream& write (const char* s, streamsize n); |
- The function accepts a character array as the first argument. It represents the data that needs to be written to the file.
- The second parameter ‘n’ represents the size of the stream. The ‘n’ characters are written from the character array to the file.
If the file is open in binary mode, the write()
function writes the characters in the binary format.
Let us see the code that writes a string to a binary file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { fstream file; file.open("binSample.dat", ios_base::out|ios_base::binary); if(!file.is_open()) { cout<<"Unable to open the file\n"; return 0; } string myStr = "This string is written to the binary file."; file.write(myStr.data(), myStr.size()); file.close(); return 0; } |
Note that code uses the data()
function of the string object. This is because the write()
function accepts the character pointer rather than the string object. The data()
function returns a pointer to an array that contains exactly the same elements as the string.
Also, you should try to close the file after using it by calling the close()
function. However, even if you forget, the destructor closes the file after the fstream
object goes out of scope.
Output:
Further reading:
Writing the Binary File Using the put() Function of fstream Library
The fstream
library inherits the put()
function from the ostream
library. The put()
function writes one character at a time to a file. Therefore, you can use it to write characters to a binary file.
If you have multiple characters, an array of characters, or a stream, you will need a loop to write characters to a binary file using the put()
function.
The definition of the put()
function is given below.
1 2 3 |
ostream& put (char c); |
After opening a file, you can simply call the put()
function using the fstream
object and pass the character to it. The character is written to the binary file.
Let us see the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { fstream file; file.open("binSample_2.dat", ios_base::out | ios_base::binary); if(!file.is_open()) { cout<<"Unable to open the file\n"; return 0; } string myStr = "This string is written to the binary file."; for(int i=0;i<myStr.size();i++) { file.put(myStr[i]); } file.close(); return 0; } |
Output:
Writing the Binary File Using Insertion Operator (<<) Of fstream Library
Similar to writing to the text file, you can write to the binary file using the insertion operator (<<). This operator is overloaded in the ostream
class and inherited in the fstream
class.
You can use the insertion operator (<<) in a similar manner as you use for writing to the console. However, instead of cout
you would use the fstream
object.
Let us see the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { fstream file; file.open("binSample_3.dat", ios_base::out|ios_base::binary); if(!file.is_open()) { cout<<"Unable to open the file\n"; return 0; } string myStr = "This string is written to the binary file."; file<<myStr; file.close(); return 0; } |
Output:
Writing the Structure Object to Binary File Using fstream Library
You can write the complex structure objects to the binary files using the write()
function of the fstream
library.
The only difference is that you can not pass the structure object directly to write()
function. You have to cast the structure object reference to a character pointer. You can do so by a simple explicit cast in C++.
Let us see the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <iostream> #include <fstream> #include <string> using namespace std; struct myStruct { string name; string gender; }; int main() { fstream file; file.open("binSample_4.dat", ios_base::out|ios_base::binary); if(!file.is_open()) { cout<<"Unable to open the file\n"; return 0; } myStruct obj; obj.name = "Mohtashim Nawaz"; obj.gender = "male"; file.write((char*)&obj, sizeof(obj)); file.close(); return 0; } |
Output:
Conclusion
Binary files store the data in binary format instead of storing it in text format. However, when opened in text editors, it generally shows data in text format.
While reading a binary file, it is mandatory that you know the structure of the file. Otherwise, the content may be illegible. That’s why the binary files are mostly used with images, network operations, etc.
That’s all about how to write binary files in C++.
Hope you have enjoyed reading the article. Stay tuned for more articles. Happy Learning!