Invisible streaming?

P

Poldanziern

Is there such a thing in C++ as stream which you can pass data into
and then retrieve data out of on the other end (without interacting
with the user or a file)?

For example, I'd like to extract a bunch of numbers, separated by
whitespace from a string ... say for example:

mystring = "100 2.0 666 3.14";

I realize that if the string contained only one number, I could use
atoi(). If it was reading in from a file, I could use myfilestream >>
a >> b >> c >> d;.

So basically I'm wondering if there's such a thing as a type of stream
object such that I could feed such a string into on one end and then
extract the variables out of the other end.

Like this:

stream mystream;
mystream << mystring;
mystream >> a >> b >> c >> d;

I know I could achieve this by streaming it out to a file and then
streaming it back in; I just want to know if there's a way to skip the
middle stage.

-Keith
 
J

Jerry Coffin

Is there such a thing in C++ as stream which you can pass data into
and then retrieve data out of on the other end (without interacting
with the user or a file)?

From your description, you want to look up std::stringstream.
 
P

Poldanziern

From your description, you want to look up std::stringstream.

From a preliminary Google search, I believe that is exactly what I was
looking for. Thanks!

-Keith
 
A

Andre Kostur

Is there such a thing in C++ as stream which you can pass data into
and then retrieve data out of on the other end (without interacting
with the user or a file)?

For example, I'd like to extract a bunch of numbers, separated by
whitespace from a string ... say for example:

mystring = "100 2.0 666 3.14";

I realize that if the string contained only one number, I could use
atoi(). If it was reading in from a file, I could use myfilestream >>
a >> b >> c >> d;.

So basically I'm wondering if there's such a thing as a type of stream
object such that I could feed such a string into on one end and then
extract the variables out of the other end.

Like this:

stream mystream;
mystream << mystring;
mystream >> a >> b >> c >> d;

I know I could achieve this by streaming it out to a file and then
streaming it back in; I just want to know if there's a way to skip the
middle stage.

Close....

#include <sstream>
#include <string>

std::string mystring = "100 2.0 666 3.14";
std::istringstream mystream(mystring.c_str());

mystream >> a >> b >> c >> d;


(OK... assuming that a-d are appropriately typed....)
 
R

Rolf Magnus

Andre said:
Close....

#include <sstream>
#include <string>

std::string mystring = "100 2.0 666 3.14";
std::istringstream mystream(mystring.c_str());

That constructor wants a string, not a pointer to char.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,369
Latest member
FTMZ

Latest Threads

Top