-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryFile.cpp
More file actions
63 lines (53 loc) · 1.27 KB
/
Copy pathbinaryFile.cpp
File metadata and controls
63 lines (53 loc) · 1.27 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
56
57
58
59
60
61
62
63
#include <iostream>
#include <fstream>
int WriteIntFile(const char* fName) {
std::ofstream fout(fName, std::ios::binary);
if (!fout) {
std::cerr << "Failed to create file.\n";
}
int i;
std::cout << "Enter data to write to file (-1 to end): ";
std::cin >> i;
while (i != -1) {
fout.write((const char*)&i, sizeof(int));
std::cin >> i;
}
return 0;
}
int ReadIntFile(const char* fName) {
std::fstream fio(fName, std::ios::binary | std::ios::in);
if (!fio) {
std::cerr << "Failed to open file\n";
return -1;
}
int i;
fio.read((char*)&i, sizeof(int));
while (!fio.eof()) {
std::cout << i << "\n";
fio.read((char*)&i, sizeof(int));
}
return 0;
}
int ReadIntFileRev(const char* fName) {
std::fstream fio(fName, std::ios::binary | std::ios::in);
if (!fio) {
std::cerr << "Failed to open file\n";
return -1;
}
fio.seekg(0, std::ios::end);
int n = fio.tellg() / sizeof(int);
std::cout << "There are " << n << " integers in file.\n";
int no = 100;
for (int i = n - 1; i >= 0; --i) {
fio.seekg(sizeof(int) * i, std::ios::beg);
fio.read((char*)&no, sizeof(int));
std::cout << no << "\n";
}
return 0;
}
int main() {
WriteIntFile("a");
ReadIntFile("a");
ReadIntFileRev("a");
return 0;
}