Skip to content

Instantly share code, notes, and snippets.

@molpopgen
Last active February 20, 2024 06:33
Show Gist options
  • Save molpopgen/9123133 to your computer and use it in GitHub Desktop.
Save molpopgen/9123133 to your computer and use it in GitHub Desktop.
Basics of binary I/O in C++
/*
Example of how to write binary stuff in C++
Illustrates some of the various quirks/annoyances
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main( int argc, char ** argv)
{
string X("Hi there");
unsigned Y = 11;
vector<double> Z(10,1.0);
ofstream o("binary_out_test.bin",ios_base::binary);
//To write a string, print out its length, and then the char *
//However, X.size() is a temp, and you cannot write a temp,
//so you need a new variable.
unsigned Xsize = X.size();
//The next line is how you write most types: cast a pointer to a type to a char * and give sizeof the type
o.write ( reinterpret_cast<char *>(&Xsize),sizeof(unsigned) );
o.write ( X.c_str(), Xsize*sizeof(char) );
//Write out that vector
//Take advantages of the fact that &vector<T>[0] is a pointer to type T.
//gotta write out the size first!!!
Xsize=Z.size();
o.write ( reinterpret_cast<char *>(&Xsize),sizeof(unsigned) );
o.write( reinterpret_cast<char *>(&Z[0]), Z.size()*sizeof(double) );
o.close();
//Now, read it all back in
ifstream in("binary_out_test.bin",ios_base::binary);
unsigned stringsize;
in.read( reinterpret_cast<char *>(&stringsize),sizeof(unsigned) );
//Cannot read to a string directly, must have buffer intermediate
vector<char> temp(stringsize);
in.read( reinterpret_cast<char *>(&temp[0]), stringsize*sizeof(char) );
string thestring(temp.begin(),temp.end());
unsigned vsize;
in.read( reinterpret_cast<char *>(&vsize),sizeof(unsigned) );
vector<double> thedoubles(vsize);
in.read( reinterpret_cast<char *>(&thedoubles[0]), vsize*sizeof(double) );
cout << stringsize << ' ' << thestring << '\n'
<< vsize << ' ';
for(unsigned i = 0 ; i < thedoubles.size() ; ++i )
{
cout << thedoubles[i] << ' ';
}
cout << '\n';
in.close();
}
@Ziaeemehr
Copy link

Nice! it solved my searching for a few hours

@icameling
Copy link

Nice! it solved my searching for a few hours, too!

@PaulKovalov
Copy link

Nice! it solved my searching for a few hours, too!

@sbrki
Copy link

sbrki commented Oct 21, 2019

Nice! it solved my searching for a few hours, too!

@byCodeXp
Copy link

byCodeXp commented Aug 2, 2020

too!

@krxyt
Copy link

krxyt commented Aug 11, 2021

Nice! it solved my searching for a few hours, too!

@djiann
Copy link

djiann commented Apr 29, 2022

cool! great thanks.

@diselyuti
Copy link

Nice! it solved my searching for a few hours, too!

@Anime-pdf
Copy link

Nice! it solved my searching for a few hours, too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment