- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_read_write.cpp
More file actions
Latest commit
45 lines (37 loc) · 1.03 KB
/
Copy pathbinary_read_write.cpp
File metadata and controls
45 lines (37 loc) · 1.03 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
#include<fstream>
#include<iostream>
usingnamespacestd;
intread_binary_file(string path, char** data){
ifstream fin;
fin.open(path.c_str(), ios_base::binary);
if(!fin.is_open()){
return -1;
}
fin.seekg(0, ios_base::end);
int length = fin.tellg();
fin.seekg(0, ios_base::beg);
*data = newchar[length];
fin.read(*data, length);
fin.close();
return length;
}
intwrite_binary_file(string path, constchar* const data, constint length){
ofstream fout;
fout.open(path.c_str(), ios_base::binary|ios_base::trunc);
if(!fout.is_open()){
return -1;
}
fout.write(data, length);
fout.close();
return length;
}
intmain(){
string lyrics = "Didn't even really wanna go\nBut if you get me out, you get a show\n";
string filename = "down_lyrics.txt";
write_binary_file(filename, lyrics.c_str(), lyrics.length());
char* data;
int len = read_binary_file(filename, &data);
for(int i = 0; i < len; i++)
cout << data[i];
return0;
}