Introduction

備忘録。
本当によく忘れるので。
C#なら読み書きは最短一行なのに…

Resolution

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
#include <cstdint>
#include <iostream>
#include <fstream>
#include <vector>

void readFile(const char* filename, std::vector<uint8_t>& vec)
{
std::ifstream file(filename, std::ios::binary);
file.unsetf(std::ios::skipws);

std::streampos fileSize;
file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);

vec.reserve(fileSize);
vec.insert(vec.begin(),
std::istream_iterator<uint8_t>(file),
std::istream_iterator<uint8_t>());

return vec;
}

void writeFile(const char* filename, std::vector<uint8_t>& vec)
{
std::ofstream file(filename, std::ios::out | std::ios::binary);
file.write((char*)&vec[0], vec.size() * sizeof(vec[0]));
file.close();
}