Add a function to get the content of a file.

This commit is contained in:
Matthieu Gautier 2018-03-27 16:36:53 +02:00
parent 2164faba44
commit 9f86b59d1d
2 changed files with 15 additions and 0 deletions

View File

@ -51,6 +51,7 @@ string appendToDirectory(const string& directoryPath, const string& filename);
unsigned int getFileSize(const string& path);
string getFileSizeAsString(const string& path);
string getFileContent(const string& path);
bool fileExists(const string& path);
bool makeDirectory(const string& path);
bool copyFile(const string& sourcePath, const string& destPath);

View File

@ -188,6 +188,20 @@ string getFileSizeAsString(const string& path)
return convert.str();
}
string getFileContent(const string& path)
{
std::ifstream f(path, std::ios::in|std::ios::ate);
std::string content;
if (f.is_open()) {
auto size = f.tellg();
content.reserve(size);
f.seekg(0, std::ios::beg);
content.assign((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
}
return content;
}
bool fileExists(const string& path)
{
#ifdef _WIN32