Last active
July 12, 2016 01:09
-
-
Save zachwhaley/9685332 to your computer and use it in GitHub Desktop.
File slurping, C++ stye
This file contains 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
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