Skip to content

Instantly share code, notes, and snippets.

@grogy
Last active December 6, 2022 07:59
Show Gist options
  • Save grogy/5137385 to your computer and use it in GitHub Desktop.
Save grogy/5137385 to your computer and use it in GitHub Desktop.
C++ read from binary file.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream in;
in.open("in.bin", ios::in | ios::binary);
// check open file for write
if (!in.is_open()) {
cerr << "Error in open file './in.bin'" << endl;
return 1;
}
// read integer - '111' in hex is '6f'
int num;
in.read((char*)&num, sizeof(num));
cout << "Integer: " << num << endl;
// read char - 'e' in hex is '65'
char ch;
in.read((char*)&ch, sizeof(ch));
cout << "Char: " << ch << endl;
// close
in.close();
return 0;
}
@peraperkan
Copy link

Thank you for this <3

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