Skip to content

Instantly share code, notes, and snippets.

@Naltharial
Created January 28, 2014 19:00
Show Gist options
  • Save Naltharial/8673915 to your computer and use it in GitHub Desktop.
Save Naltharial/8673915 to your computer and use it in GitHub Desktop.
int fileToString(const char *filename, std::string& s)
{
size_t size;
char* str;
std::fstream f(filename, (std::fstream::in | std::fstream::binary));
if (f.is_open())
{
size_t fileSize;
f.seekg(0, std::fstream::end);
size = fileSize = (size_t)f.tellg();
f.seekg(0, std::fstream::beg);
str = new char[size + 1];
if (!str)
{
f.close();
return SDK_SUCCESS;
}
f.read(str, fileSize);
f.close();
str[size] = '\0';
s = str;
delete[] str;
return SDK_SUCCESS;
}
std::cout << "Error: failed to open file\n:" << filename << std::endl;
return SDK_FAILURE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment