-
Notifications
You must be signed in to change notification settings - Fork 4
/
read_write_binary_file.cpp
47 lines (37 loc) · 1.15 KB
/
read_write_binary_file.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
char* as_bytes(int& i);
// Read and write binary file of integers.
int main(int argc, char* argv[]) {
if (argc < 3) {
cout << "Usage: " << argv[0] << " input_file output_file\n";
return 0;
}
// open an istream for binary input from a file:
// note: stream mode binary tells the stream not to try anything clever with the bytes
ifstream ifs(argv[1], ios_base::binary);
if (!ifs) {
cerr << "can't open input file " << argv[1] << endl;
return 1;
}
// open an ostream for binary output to a file:
ofstream ofs(argv[2], ios_base::binary);
if (!ofs) {
cerr << "can't open output file " << argv[2] << endl;
return 1;
}
vector<int> v;
// read from binary file:
for (int x; ifs.read(as_bytes(x), sizeof(int));) // note: reading bytes
v.push_back(x);
// ... do something with v ...
// write to binary file:
for (int x : v)
ofs.write(as_bytes(x), sizeof(int)); // note: writing bytes
return 0;
}
char* as_bytes(int& i) {
return reinterpret_cast<char*>(&i);
}