std string streams

D

Dylan

Is there a string stream I can write into and read from?

I've tried std::stringstream but it won't let me read from it.

ie I can do this:

int iData = 123
std::stringstream ss;
ss << 123;

but not this afterwards:

int iDataReadFromStream;
iDataReadFromStream << ss;


Is there a data structure that allows me to do both?

thanks
 
S

Sebastian Hungerecker

Dylan said:
ie I can do this:

int iData = 123
std::stringstream ss;
ss << 123;

but not this afterwards:

int iDataReadFromStream;
iDataReadFromStream << ss;

I'm no expert, but I'd say the latter doesn't work, because it calls the
<<-operator for int and not for stringstream.
Try:
ss>>iDataReadFromStream;
 
R

Richard Herring

Dylan said:
Is there a string stream I can write into and read from?

I've tried std::stringstream but it won't let me read from it.

ie I can do this:

int iData = 123
std::stringstream ss;
ss << 123;

but not this afterwards:

int iDataReadFromStream;
iDataReadFromStream << ss;

ss >> iDataReadFromStream;

Streams always go on the left.
 
D

Dylan

I'm no expert, but I'd say the latter doesn't work, because it calls the
<<-operator for int and not for stringstream.
Try:
ss>>iDataReadFromStream;

that doesn't work either...
 
J

Joe Bacigalupa

maybe your thinking of a situation like this...

const int MaxSize = 1023;
std::stringstream convert;
char temp[MaxSize + 1] ={0};
float val;

// Say we have a control that has a "text" floating point value
GetDlgItemText(IDC_SOME_CONTROL, temp, MaxSize);

convert << temp;
convert >> val;

// if you forgot to call this afterward, you would have issues writing or
reading from the stringstream
convert.clear();
convert.str("");
 

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
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top