istringstream problem

D

DerekBaker

Why does this code only give Value = "Microsoft":

istringstream is(Dest);
is >> Value;

where Value is a string& and Dest a char* holding "Microsoft Sans Serif"
 
J

Juha Nieminen

DerekBaker said:
Why does this code only give Value = "Microsoft":

istringstream is(Dest);
is >> Value;

where Value is a string& and Dest a char* holding "Microsoft Sans Serif"

Because ">> std::string" is defined to read the next
whitespace-delimited word into the string.
 
J

Jim Langston

DerekBaker said:
Why does this code only give Value = "Microsoft":

istringstream is(Dest);
is >> Value;

where Value is a string& and Dest a char* holding "Microsoft Sans Serif"

As stated, operator<< for stream and std::string is deliminated by
whitespace. A space, end of line, etc...
If you want the entire line use getline.
std::getline( is, Value );
which is delimited by the end of line.
 
D

DerekBaker

* Jim Langston:
As stated, operator<< for stream and std::string is deliminated by
whitespace. A space, end of line, etc...
If you want the entire line use getline.
std::getline( is, Value );
which is delimited by the end of line.

The function the code's in is templated for Value. I guess I'll have to
specialize the function for string.
 
J

Jim Langston

DerekBaker said:
* Jim Langston:

The function the code's in is templated for Value. I guess I'll have to
specialize the function for string.

Usually an arbitrary value is delimited by whitespace anyway, with some
exceptions.

I've found that for std::string sometimes I need one word, sometimes I need
the entire line, it depends on what I am specifically doing at the moment.
As soon as you specialize for std::string to get the entire line you'll find
that you now only need to get one one word.
 

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,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top