Skip to content

Commit a9c4774

Browse files
committed
fix(files): Sometimes not reading
1 parent 84e4de5 commit a9c4774

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/files/Files.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ std::string Files::Read(std::string path)
2424
if (!std::filesystem::exists(path))
2525
return "";
2626

27-
std::ifstream f(path);
28-
std::stringstream strStream;
29-
strStream << f.rdbuf();
30-
return strStream.str();
27+
auto fp = std::fopen(path.c_str(), "rb");
28+
std::string s;
29+
std::fseek(fp, 0u, SEEK_END);
30+
auto size = std::ftell(fp);
31+
std::fseek(fp, 0u, SEEK_SET);
32+
s.resize(size);
33+
std::fread(&s[0], 1u, size, fp);
34+
std::fclose(fp);
35+
return s;
3136
}
3237

3338
std::string Files::getBase(std::string filePath)
@@ -52,7 +57,7 @@ void Files::Append(std::string path, std::string content, bool hasdate)
5257
ChangePath();
5358
std::filesystem::create_directories(Files::getBase(path));
5459
time_t now = time(0);
55-
tm *ltm = localtime(&now);
60+
tm* ltm = localtime(&now);
5661

5762
char date[32];
5863

@@ -75,7 +80,7 @@ void Files::Write(std::string path, std::string content, bool hasdate)
7580
ChangePath();
7681
std::filesystem::create_directories(Files::getBase(path));
7782
time_t now = time(0);
78-
tm *ltm = localtime(&now);
83+
tm* ltm = localtime(&now);
7984

8085
char date[32];
8186

@@ -122,7 +127,7 @@ std::vector<std::string> Files::FetchFileNames(std::string path)
122127
if (!IsDirectory(path))
123128
return files;
124129

125-
for (const auto &entry : std::filesystem::directory_iterator(path))
130+
for (const auto& entry : std::filesystem::directory_iterator(path))
126131
{
127132
if (entry.is_directory())
128133
{
@@ -146,7 +151,7 @@ std::vector<std::string> Files::FetchDirectories(std::string path)
146151
if (!IsDirectory(path))
147152
return directories;
148153

149-
for (const auto &entry : std::filesystem::directory_iterator(path))
154+
for (const auto& entry : std::filesystem::directory_iterator(path))
150155
if (entry.is_directory())
151156
directories.push_back(entry.path().string());
152157

0 commit comments

Comments
 (0)