-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.hh
48 lines (40 loc) · 1.32 KB
/
io.hh
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
//
// Created by JianNan Tian on 2019-08-27.
//
#ifndef IO_HH
#define IO_HH
#include <fstream>
#include <iostream>
namespace io {
// template <typename T>
// void read_binary_file(T* const __a, size_t const __len, std::string const* __name) {
// std::ifstream ifs(__name->c_str(), std::ios::binary | std::ios::in);
// if (not ifs.is_open()) {
// std::cerr << "fail to open " << *__name << std::endl;
// return;
// }
// ifs.read(reinterpret_cast<char*>(__a), std::streamsize(__len * sizeof(T)));
// ifs.close();
//}
template <typename T>
T* ReadBinaryFile(const std::string& __name, size_t __len) {
std::ifstream ifs(__name.c_str(), std::ios::binary | std::ios::in);
if (not ifs.is_open()) {
std::cerr << "fail to open " << __name << std::endl;
exit(1);
// return;
}
auto __a = new T[__len]();
ifs.read(reinterpret_cast<char*>(__a), std::streamsize(__len * sizeof(T)));
ifs.close();
return __a;
}
template <typename T>
void WriteBinaryFile(T* const __a, size_t const __len, std::string const* const __name) {
std::ofstream ofs(__name->c_str(), std::ios::binary | std::ios::out);
if (not ofs.is_open()) return;
ofs.write(reinterpret_cast<const char*>(__a), std::streamsize(__len * sizeof(T)));
ofs.close();
}
} // namespace io
#endif // IO_HH