-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_copy.cpp
48 lines (36 loc) · 1.13 KB
/
file_copy.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 <iostream>
#include <fstream>
#include <memory>
#include <string>
#include <vector>
template <typename T,unsigned S>
inline unsigned arraysize(const T (&v)[S]) { return S; }
int main(int argc, char const *argv[])
{
int buffer_size = 1024;
if( argc < 3)
{
std::cerr<< "Syntax: file_copy <in_file> <out_file>"<<std::endl;
return -1;
}
std::string in_filename(argv[1]);
std::string out_filename(argv[2]);
std::ifstream in_file(in_filename,std::ifstream::binary);
std::ofstream out_file(out_filename,std::ofstream::binary|std::ofstream::trunc);
in_file.seekg (0, in_file.end);
int file_length = in_file.tellg();
in_file.seekg (0, in_file.beg);
int loaded_byte = 0;
int byte_to_read = std::min(buffer_size,file_length-loaded_byte);
std::vector<char> buffer(buffer_size + 1,0);
while(in_file.read(buffer.data(),byte_to_read)&&byte_to_read)
{
//std::copy( noise.begin(), noise.end(), std::back_inserter(buffer_noised));
out_file.write(buffer.data(),byte_to_read);
loaded_byte += byte_to_read;
byte_to_read = std::min(buffer_size,file_length-loaded_byte);
}
in_file.close();
out_file.close();
return 0;
}