-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.cpp
More file actions
55 lines (36 loc) · 1.09 KB
/
Copy pathtesting.cpp
File metadata and controls
55 lines (36 loc) · 1.09 KB
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
55
#include <iostream>
#include <fstream>
#include <string.h>
#include <vector>
using namespace std;
void fillBufferWithStreamBinary(vector<char>& buf, string filename);
void copyBufferIntoFileBinary(vector<char>& buffer, string filename);
int main() {
//string buffer;
vector<char> buffer;
fillBufferWithStreamBinary(buffer, "dragon.jpg");
copyBufferIntoFileBinary(buffer, "dragon2.jpg");
return 0;
}
void fillBufferWithStreamBinary(vector<char>& buf, string filename) {
ifstream file(filename.c_str(), ios::in|ios::binary|ios::ate); //ios::ate sets the initial position at the end of the file
streampos size;
char* memblock;
if(file.is_open()) {
size = file.tellg();
memblock = new char[size];
file.seekg(0, ios::beg);
file.read(memblock, size);
file.close();
buf.assign(memblock, memblock+size-1);
delete[] memblock;
}
else {
cout << "Unable to open the file" << endl;
}
}
void copyBufferIntoFileBinary(vector<char>& buffer, string filename) {
ofstream file(filename.c_str(), ios::out|ios::binary);
file.write(buffer.data(), buffer.size());
file.close();
}