17std::string
readFile(
const std::string &path, std::string::size_type maxSize)
20 file.exceptions(ios_base::failbit | ios_base::badbit);
21 file.open(path, ios_base::in | ios_base::binary);
22 file.seekg(0, ios_base::end);
24 const auto size =
static_cast<string::size_type
>(file.tellg());
25 if (maxSize != string::npos && size > maxSize) {
26 throw ios_base::failure(
"File exceeds max size");
29 file.seekg(ios_base::beg);
32#pragma GCC diagnostic push
33#pragma GCC diagnostic ignored "-Wnull-dereference"
35 res.assign((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
37#pragma GCC diagnostic pop
58void writeFile(std::string_view path, std::string_view contents)
61 file.exceptions(ios_base::failbit | ios_base::badbit);
62 file.open(std::string(path), ios_base::out | ios_base::trunc | ios_base::binary);
63 file.write(contents.data(),
static_cast<std::streamoff
>(contents.size()));