-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathfile_util.cpp
More file actions
28 lines (24 loc) · 818 Bytes
/
file_util.cpp
File metadata and controls
28 lines (24 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "file_util.h"
#include <fstream>
#include <sstream>
namespace common {
std::string FileUtil::createFileWithContent(const std::string& base_dir,
const std::string& filename,
const std::string& content) {
std::string path =
base_dir + ((base_dir.back() == '/') ? filename : ("/" + filename));
std::ofstream file(path);
file << content;
file.close();
return path;
}
void FileUtil::readFileToString(const std::string& path, std::string* content) {
const std::ifstream input_stream(path);
if (input_stream.fail()) {
throw std::runtime_error("Failed to open file: " + path);
}
std::stringstream buffer;
buffer << input_stream.rdbuf();
content->assign(buffer.str());
}
} // namespace common