-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
56 lines (49 loc) · 1.6 KB
/
utils.h
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
#ifndef UTILS_H
#define UTILS_H
#include <fstream>
#include <iostream>
namespace io
{
size_t FileSize(const std::string filename)
{
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
return static_cast<size_t>(in.tellg());
}
template <typename T>
T *read_binary_to_new_array(const std::string &fname)
{
std::ifstream ifs(fname.c_str(), std::ios::binary | std::ios::in);
if (not ifs.is_open())
{
std::cerr << "fail to open " << fname << std::endl;
exit(1);
}
size_t dtype_len = FileSize(fname) / sizeof(T);
auto _a = new T[dtype_len]();
ifs.read(reinterpret_cast<char *>(_a), std::streamsize(dtype_len * sizeof(T)));
ifs.close();
return _a;
}
template <typename T>
void read_binary_to_array(const std::string &fname, T *_a, size_t dtype_len)
{
std::ifstream ifs(fname.c_str(), std::ios::binary | std::ios::in);
if (not ifs.is_open())
{
std::cerr << "fail to open " << fname << std::endl;
exit(1);
}
ifs.read(reinterpret_cast<char *>(_a), std::streamsize(dtype_len * sizeof(T)));
ifs.close();
}
template <typename T>
void write_array_to_binary(const std::string &fname, T *const _a, size_t const dtype_len)
{
std::ofstream ofs(fname.c_str(), std::ios::binary | std::ios::out);
if (not ofs.is_open())
return;
ofs.write(reinterpret_cast<const char *>(_a), std::streamsize(dtype_len * sizeof(T)));
ofs.close();
}
}
#endif // UTILS_H