Skip to content

Instantly share code, notes, and snippets.

@blippy
Last active March 1, 2021 21:06
Show Gist options
  • Save blippy/3dc25269932fa6f8da003affd2a70e68 to your computer and use it in GitHub Desktop.
Save blippy/3dc25269932fa6f8da003affd2a70e68 to your computer and use it in GitHub Desktop.
Slurp a file in C++
#include <fstream>
#include <sstream>
std::string slurp(const char *filename)
{
std::ifstream in;
in.open(filename, std::ifstream::in | std::ifstream::binary);
std::stringstream sstr;
sstr << in.rdbuf();
in.close();
return sstr.str();
}
@ts-arun
Copy link

ts-arun commented May 14, 2018

What about files with different encodings like "UCS-2 BE BOM" or so?

@blippy
Copy link
Author

blippy commented Mar 1, 2021

What about files with different encodings like "UCS-2 BE BOM" or so?

Updated to include binary. It just slurps a file, it won't do any decoding to UTF-8 or anything like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment