tobik
tobik

Reputation: 7198

C++: how do I create istream with desired content?

I was given a function with following signature. I can't change it, I have to work with it.

void parse(std::istream & in);

I'm supposed to test this function, so basically call it with predefined content and check if the values were properly parsed. Therefore I need to call this function... something like parse("abcdedf....")... but I wasn't able to to find a way how to do it.

I'm new to C++ so this may be a dumb question. As far as I understand streams, istream is something that I get when reading from a source, a file for example. So I need to turn regular string into this source but I don't know how.

Upvotes: 0

Views: 3008

Answers (1)

Qaz
Qaz

Reputation: 61910

Use a string stream:

std::istringstream iss("abcdef....");
parse(iss);

Like std::ifstream, used for reading in files, std::istringstream derives from std::istream, so you can upcast a std::istringstream & to a std::istream & to pass in.

Upvotes: 2

Related Questions