1#define CPP_UTILITIES_IOMISC_STRING_VIEW
17std::string
readFile(
const std::string &path, std::string::size_type maxSize)
19 return readFile(std::string_view(path), maxSize);
27std::string
readFile(std::string_view path, std::string_view::size_type maxSize)
30 file.exceptions(ios_base::failbit | ios_base::badbit);
31 file.open(path.data(), ios_base::in | ios_base::binary);
32 file.seekg(0, ios_base::end);
34 const auto size =
static_cast<string::size_type
>(file.tellg());
35 if (maxSize != string::npos && size > maxSize) {
36 throw ios_base::failure(
"File exceeds max size");
39 file.seekg(ios_base::beg);
42#pragma GCC diagnostic push
43#pragma GCC diagnostic ignored "-Wnull-dereference"
45 res.assign((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
47#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(path.data(), ios_base::out | ios_base::trunc | ios_base::binary);
63 file.write(contents.data(),
static_cast<std::streamoff
>(contents.size()));
Contains all utilities provides by the c++utilities library.
CPP_UTILITIES_EXPORT std::string readFile(const std::string &path, std::string::size_type maxSize=std::string::npos)
Reads all contents of the specified file in a single call.
CPP_UTILITIES_EXPORT void writeFile(std::string_view path, std::string_view contents)
Writes all contents to the specified file in a single call.
std::fstream NativeFileStream