-
-
Save datoubanzhuan/44d70cb573e79929bf698bd14904bca8 to your computer and use it in GitHub Desktop.
C++ read from binary file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment