-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_utils.cc
48 lines (40 loc) · 1.2 KB
/
file_utils.cc
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "file_utils.h"
#include <string>
using std::string;
#include <fstream>
using std::ifstream;
using std::ofstream;
#include <stdio.h>
namespace reddit {
bool DoesFileExist(const string& fname) {
ifstream in_stream(fname);
return in_stream;
}
void DeleteFile(const string& fname) {
remove(fname.c_str());
}
void WriteStringToFile(const std::string& fname, const std::string& str) {
ofstream out_stream(fname, ofstream::out | ofstream::binary | ofstream::trunc);
out_stream.write(str.data(), str.size());
out_stream.close();
}
void AppendStringToFile(const std::string& fname, const std::string& str) {
ofstream out_stream(fname, ofstream::out | ofstream::binary | ofstream::app);
out_stream.write(str.data(), str.size());
out_stream.close();
}
string ReadFileToString(const string& fname) {
ifstream in_stream(fname, ifstream::in | ifstream::binary);
if (in_stream) {
string contents;
in_stream.seekg(0, std::ios::end);
contents.resize(in_stream.tellg());
in_stream.seekg(0, std::ios::beg);
in_stream.read(&contents[0], contents.size());
in_stream.close();
return contents;
} else {
return string();
}
}
}; // namespace reddit