I am trying to write the file in binary format. I have the following code with me, but it save the file in text format.
#include <iostream>
#include <fstream>
using namespace std;
int main(){
std::string ref = "Ecoli. 123";
unsigned int size = 124;
std::ofstream supp_info_output("binary_file", std::ios::out | std::ios::binary); // saving file
supp_info_output << ref << std::endl;
supp_info_output << size << std::endl;
supp_info_output.close();
std::ifstream supp_info_input("binary_file", std::ios::in | std::ios::binary); // loading file
std::string supp_info_line;
while( std::getline( supp_info_input, supp_info_line ).good() ){
std::cout << supp_info_line << std::endl;
}
supp_info_input.close();
}
In the code I am writing some data and then reading the data again. There is no problem in reading and writing,but I need the file in binary format.
@Klaus is right, if you pass int to << in a binary file, it just writes text to file. If you want to write int binary to file with <<, you should pass the bytes stream to <<. For example, in your code, you need to write the 4 bytes of the int each time with <<, then binary int will be written to your file. You can check below code:
I think the behavior of << in binary file mode is expected, because in binary file mode everything should be treated as bytes stream regardless of its type such as int, double, short int, ... etc.
It's slightly more difficult than it looks to write data in binary. I suggest using the C FILE * interface, because many C++ programmers overload << and >> to insert and extract higher-level objects, in text mode. You lose that when you go to binary.
To insert an integer, you need to know if it is 16 bit or 32 bit, big-endian or little-endian. You then use shifts and masks and calls to fputc() to write the bytes directly. To insert an asciiz string, similarly call fputc and ensure that you write the nul.
Use ofstream::write to write binary data and ifstream::read to read them. Note, that you should save the length of strings because you should know how many bytes to read further.
And here is how to read:
As commented on answer from AwaitedOne I dislike writing a lot of code for every single output statement. Instead I prefer having a overload for shift operator to make user code a bit easier and less error prone.
As a startpoint the following example is given. There are not all possible overloads presented and also the BinaryFile class is less then "ready for production". It is only given as a compileable startpoint to start own investigations.
Keep in mind: binary file is not portable, binary representation can change from version to version of compiler/libc/other. It is also dependend of OS and endianess of system and also of the architecture ( 32 vs 64 bit ) and many others.
So binary data should not be used in general to make data persistent for "later" use unless you write handler methods, which have a defined in/out format. My example write simple the memory of the storage of the data which is very stupied and results in all the problems are mentioned. Feel free to start an own "perfect" implementation for an exchangeable binary format.
But instead of reinvent the whell, someone should use ready to use implementations given already everywhere. Search for "serializer" and you will find a good set of libs like boost serialize and many others.