-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace-head.cpp
executable file
·55 lines (43 loc) · 1.05 KB
/
replace-head.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
48
49
50
51
52
53
54
#include <cassert>
#include <fstream>
#include <iostream>
using namespace std;
void showUsage(const string &exec)
{
cout << "Usage: " << exec << " HEAD BODY OUTPUT" << endl;
cout << "\t" "(body = head + tail)" << endl;
exit(1);
}
long getSize(ifstream &stream)
{
long pos = stream.tellg();
stream.seekg(0, ifstream::end);
long size = stream.tellg();
stream.seekg(pos);
return size;
}
void copy(ifstream &src, ofstream &dest, long size)
{
char *buffer = new char[size];
src.read(buffer, size);
dest.write(buffer, size);
delete[] buffer;
}
int main(int argc, char **argv)
{
if (argc != 4)
showUsage(argv[0]);
ifstream headFile(argv[1], ifstream::binary);
ifstream bodyFile(argv[2], ifstream::binary);
if (!headFile || !bodyFile)
showUsage(argv[0]);
ofstream output(argv[3], ifstream::binary);
long headSize = getSize(headFile);
long bodySize = getSize(bodyFile);
long tailSize = bodySize - headSize;
assert(tailSize >= 0);
copy(headFile, output, headSize);
headFile.seekg(headSize);
copy(bodyFile, output, tailSize);
return 0;
}