とある技術者の劣等感
備忘録。本当によく忘れるので。C#なら読み書きは最短一行なのに…
1234567891011121314151617181920212223242526272829
#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();}