memort stream

Brad Garcia garsh@home.com
Thu Oct 4 12:57:00 GMT 2001


Levente Farkas wrote:
> not exactly:-( suppose I'd like to read a binary file's content into
> the memory once (to save file i/o operation in order to make it faster)
> than a factory create object which can initialize itself from the 
> binary stream. so I'd like to give the factory a stream which has 
> the content of the binary file. currently iy's not solvable with
> stringstream (but strstream), since istringstream.rdbuf()->pubsetbuf(..)
> do nothing:-( so what is the solution?

I think stringstream will work.

Here's a little example program that makes a copy of a file by first
copying the entire file into a stringstream, and then copying it
from the stringstream to the output file:

#include <fstream>
#include <sstream>

int main()
{
    std::stringstream memstream;
    std::ifstream infilestream("conf");
    memstream << infilestream.rdbuf();
    infilestream.close();

    std::ofstream outfilestream("conf2");
    outfilestream << memstream.rdbuf();
    outfilestream.close();
}


Brad Garcia



More information about the Libstdc++ mailing list