-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathprint64.cpp
46 lines (37 loc) · 1.11 KB
/
print64.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
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdint>
#include <cstdlib>
int main(int argc, char** argv) {
if (argc < 2) {
std::cerr << "Usage: ./print64 <filename>" << std::endl;
exit(EXIT_FAILURE);
}
FILE * f = fopen(argv[1], "rb");
if (f == NULL) {
std::cerr << "Error opening file `" << argv[1] << "`." << std::endl;
exit(EXIT_FAILURE);
}
// get file size
fseek(f, 0, SEEK_END);
size_t size = ftell(f);
rewind(f);
if (size % 8 != 0) {
std::cerr << "Error: file `" << argv[1] << "` has size which is not a multiple of 64bits." << std::endl;
exit(EXIT_FAILURE);
}
// read file
size_t count = size/sizeof(uint64_t);
std::vector<uint64_t> buf(count);
size_t read_count = fread(&buf[0], sizeof(uint64_t), count, f);
if (read_count != count) {
std::cerr << "Unexpected error reading file." << std::endl;
exit(EXIT_FAILURE);
}
// output file in decimal format
for (size_t i = 0; i < buf.size(); ++i) {
std::cout << buf[i] << std::endl;
}
return 0;
}