c++ - Working with file streams generically -
i want work file streams generically. is, want 'program interface , not implementation'. this:
ios * genericfileio = new ifstream("src.txt"); getline(genericfileio, somestringobject);//from string library; dont want use c strings genericfileio = new ofstream("dest.txt"); genericfileio -> operator<<(somestringobject);
is possible? not great inheritance. given io class hierarchy, how implement want?
do mean:
void pass_a_line(std::istream& in, std::ostream& out) { // error handling left exercise std::string line; std::getline(in, line); out << line; }
this can work std::istream
, std::ostream
, so:
// file cout // no need new std::ifstream in("src.txt"); pass_a_line(in, std::cout); // stringstream file std::istringstream stream("hi"); std::ofstream out("dest.txt"); pass_a_line(stream, out);
this example do, , programmed against std::istream
, std::ostream
interfaces. that's not generic programming; that's object oriented programming.
boost.iostreams can adapt classes std::[i|o|io]stream
s, , using generic programming.
Comments
Post a Comment