Skip to content

Commit 0effdfe

Browse files
committed
Added readtextFile
1 parent ed38981 commit 0effdfe

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/wsjcpp_core.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <thread>
1616
#include <cstdint>
1717
#include <unistd.h>
18+
#include <streambuf>
1819

1920
// ---------------------------------------------------------------------
2021

@@ -282,7 +283,7 @@ bool WSJCppCore::makeDir(const std::string &sDirname) {
282283

283284
bool WSJCppCore::writeFile(const std::string &sFilename, const std::string &sContent) {
284285

285-
std::ofstream f(sFilename, std::ios::out);
286+
std::ofstream f(sFilename, std::ifstream::in);
286287
if (!f) {
287288
std::cout << "FAILED could not create file to wtite " << sFilename << std::endl;
288289
return false;
@@ -295,6 +296,24 @@ bool WSJCppCore::writeFile(const std::string &sFilename, const std::string &sCon
295296

296297
// ---------------------------------------------------------------------
297298

299+
bool WSJCppCore::readTextFile(const std::string &sFilename, std::string &sContent) {
300+
301+
std::ifstream f(sFilename);
302+
if (!f) {
303+
std::cout << "FAILED could not open file to read " << sFilename << std::endl;
304+
return false;
305+
}
306+
307+
sContent = std::string(
308+
(std::istreambuf_iterator<char>(f)),
309+
std::istreambuf_iterator<char>()
310+
);
311+
312+
return true;
313+
}
314+
315+
// ---------------------------------------------------------------------
316+
298317
bool WSJCppCore::writeFile(const std::string &sFilename, const char *pBuffer, const int nBufferSize) {
299318
std::ofstream f(sFilename, std::ios::out | std::ios::binary);
300319
if (!f) {
@@ -307,6 +326,7 @@ bool WSJCppCore::writeFile(const std::string &sFilename, const char *pBuffer, co
307326
}
308327

309328

329+
310330
// ---------------------------------------------------------------------
311331

312332
std::string& WSJCppCore::ltrim(std::string& str, const std::string& chars) {

src/wsjcpp_core.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ class WSJCppCore {
3636
static std::vector<std::string> listOfFiles(const std::string &sDirname);
3737
static bool makeDir(const std::string &sDirname);
3838
static bool writeFile(const std::string &sFilename, const std::string &sContent);
39+
static bool readTextFile(const std::string &sFilename, std::string &sOutputContent);
3940
static bool writeFile(const std::string &sFilename, const char *pBuffer, const int nBufferSize);
41+
4042

4143
static std::string& ltrim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
4244
static std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
@@ -98,4 +100,4 @@ class WSJCppLog {
98100
static void add(WSJCppColorModifier &clr, const std::string &sType, const std::string &sTag, const std::string &sMessage);
99101
};
100102

101-
#endif // WSJCPP_CORE_H
103+
#endif // WSJCPP_CORE_H

0 commit comments

Comments
 (0)