Sep 22, 2018 at 3:45pm UTC
Hi guys, I have this piece of code which reads a file and put all its content into a string. Is there a better way to do this or is the current code I have acceptable?
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
#ifndef CFILE_H
#define CFILE_H
#include <iostream>
#include <fstream>
class CFILE {
private :
std::ifstream _in;
//std::ofstream _out;
std::string _key;
std::string _data;
bool g_d();
public :
CFILE(std::string file_name, std::string key);
};
#endif
CFILE::CFILE(std::string file_name, std::string key) {
_key = key;
_in.open(file_name);
if (_in.is_open())
g_d();
}
// private functions
bool CFILE::g_d() {
std::string i;
while (getline(_in, i)){ _data += i; }
return true ;
}
Last edited on Sep 22, 2018 at 3:47pm UTC
Sep 22, 2018 at 7:16pm UTC
Thanks Thomas. This link helps allot.