Created
January 13, 2019 00:06
-
-
Save arrieta/a98a7424dce11f9c9b0083c30abbc1db to your computer and use it in GitHub Desktop.
Read file into std::string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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