I would like to extend ifstream by defining a new class ifstreamExt where :
there is a class variable called 'directory' that is a string and that holds the location where the file will be opened.
the open method is redefined so to use the
directoryvariable.and finally,
>>is supposed to work with string at least.
I managed to do 1 & 2 - but I am failing with 3. I do not know how to write it, I made multiple trials. The first one was using getline, it worked fine, but not as >> works ; this operator skips blanks and retrieve consecutive non-blanks character, whereas getline gets the full line and does not pick each of sub-string.
class ifstreamExt : ifstream {
public:
void open(string f) {
string s = h + "/" + f;
name = s;
if (!is_open()) {
cerr << "cannot open " << s << endl;
exit(1);
} else {
cerr << "open file " << s << endl;
}
}
friend ifstreamExt & operator >> (ifstreamExt & is, string &str) {
}
static void set_directory(string d) {
cerr << "set home directory : " << d << endl;
h = d;
DIR* dir = opendir(h.c_str());
if (dir) {
cerr << "directory " << h << " exists " << endl;
} else {
cerr << "directory " << h << " does not exist " << endl;
exit(1);
}
}
private :
static string h;
};
Following the recommendations posted in the comments, here is ifstreamExt which is a new class with composition. Any feedback welcome !