-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIO.cpp
More file actions
34 lines (27 loc) · 779 Bytes
/
IO.cpp
File metadata and controls
34 lines (27 loc) · 779 Bytes
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
#include "IO.hpp"
#include <array>
#include <fstream>
template <class Stream>
Stream openFile(const char *path) {
Stream s(path, std::ios::binary);
if (!s.is_open()) {
throw std::runtime_error("Can't open file");
}
return s;
}
std::vector<char> readFromFile(const char *path) {
auto in = openFile<std::ifstream>(path);
std::vector<char> data;
std::array<char, 4096> buffer{};
std::streamsize bytes{1};
while (bytes > 0) {
in.read(buffer.data(), buffer.size());
bytes = in.gcount();
data.insert(data.end(), buffer.data(), buffer.data() + bytes);
}
return data;
}
void writeToFile(const char *path, const std::vector<char> &data) {
openFile<std::ofstream>(path).write(
data.data(), static_cast<std::streamsize>(data.size()));
}