PeeWee2201
PeeWee2201

Reputation: 1534

Wraps a vector<unsigned char> inside a stream?

I have a function which takes as argument an std::istream& and writes a transformed stream to an std::ostream&.

On the another hand, I have another function which accepts a vector argument.

My goal is to pass the output of the first function to the second function.

Is there something out of the box to do that ? Otherwise, how can I easily implement it ?

Thank you

Edit : here are the 2 functions signature :

functionA(std::istream& _in, std::ostream& _out);
functionB(std::vector<unsigned char>& data);

The caller would look like :

std::vector<unsigned char> data;
std::istrstream stream_in("input message");
std::ovectorstream stream_out(data); // ???
functionA(stream_in, stream_out);
functionB(stream_out.vector());

Upvotes: 3

Views: 2063

Answers (1)

RedX
RedX

Reputation: 15175

I think something like this might work

std::vector<unsigned char> data;
std::istringstream stream_in("input message");
std::stringstream stream_out;
functionA(stream_in, stream_out);
const std::string& str_out(stream_out.str());
copy(str_out.begin(), str_out.end(), std::back_inserter(data));
functionB(data);

Upvotes: 5

Related Questions