0

I'm working on function that manipulate a stringbuf and I need to keep this in a file. If I could pass the stringbuf directly to ofstream's filebuf that could be a huge advantage.

I know that both use streambuf object than can I somehow replace the filebuf's streambuf to my stringbuf's stream?

Take a look to the code below to better understand the idea:

#include <fstream>
#include <sstream>

void printit(std::stringbuf &MyBb) {
    std::ofstream fFile("newfile.txt");

    fFile.rdbuf(MyBb.rdbuf()); //MyBb doesn't have this member function
                               //How can I give to fFile the streambuf
                               //from my stringbuf?  
}

int main() {
    std::stringbuf MyB;

    MyB.sputn("First sentence\n",15);

    printit(MyB);

    return 0;
}
TheArchitect
  • 1,160
  • 4
  • 15
  • 26

1 Answers1

-2

You just need to do like you were writing a string to it and use your function. Use fFile << MyPb.rdBuff(); That should probably work