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]streams, , using generic programming.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -