Skip to content

Instantly share code, notes, and snippets.

@zachwhaley
Last active July 12, 2016 01:09
Show Gist options
  • Save zachwhaley/9685332 to your computer and use it in GitHub Desktop.
Save zachwhaley/9685332 to your computer and use it in GitHub Desktop.
File slurping, C++ stye
std::string slurp(const std::string& filename)
{
// Open the file into an input file stream
std::ifstream ifile(filename, std::ios::in | std::ios::binary | std::ios::ate);
// Check that the file exists
if (ifile) {
std::string str;
// Move to the end of the file
ifile.seekg(0, std::ios::end);
// Resize the string to the length of the file.
str.resize(ifile.tellg());
// Move back to the beginning of the file
ifile.seekg(0, std::ios::beg);
// Read the entire file into str
ifile.read(&str.front(), str.size());
return str;
}
throw;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment