Skip to content

Instantly share code, notes, and snippets.

@arrieta
Created January 13, 2019 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arrieta/a98a7424dce11f9c9b0083c30abbc1db to your computer and use it in GitHub Desktop.
Save arrieta/a98a7424dce11f9c9b0083c30abbc1db to your computer and use it in GitHub Desktop.
Read file into std::string
#pragma once
#include <fstream>
#include <string>
#include <system_error>
#include <iterator>
inline std::string slurp(const std::string& path) {
constexpr auto flags = std::ios::in | std::ios::binary;
if (auto fp = std::ifstream(path, flags); fp) {
return {std::istreambuf_iterator<char>{fp},
std::istreambuf_iterator<char>{}};
} else {
throw std::system_error(errno, std::system_category(), path);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment