By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,006 Members | 1,238 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,006 IT Pros & Developers. It's quick & easy.

Using std::ws

P: n/a
Hi,

I am having a hard time understanding how to use std::ws. I am
trying to parse a simple string (*). My goal would be that 'st'
contains 'Hello World'. I thought that using std::ws would indeed do
what I was looking for.

Thanks for suggestion,
-Mathieu

(*)
#include <iostream>
#include <sstream>

int main(int, char *[])
{
const char s[] = "1 2 3 Hello World";
std::istringstream is(s);
int a,b,c;
std::string st;
is >a;
is >b;
is >c;
is >std::ws;
is >st;
std::cout << a << " " << b << " " << c << " " << st << std::endl;

return 0;
}

Jul 13 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
mathieu wrote:
Hi,

I am having a hard time understanding how to use std::ws. I am
trying to parse a simple string (*). My goal would be that 'st'
contains 'Hello World'. I thought that using std::ws would indeed do
what I was looking for.

Thanks for suggestion,
-Mathieu

(*)
#include <iostream>
#include <sstream>

int main(int, char *[])
{
const char s[] = "1 2 3 Hello World";
std::istringstream is(s);
int a,b,c;
std::string st;
is >a;
is >b;
is >c;
is >std::ws;
is >st;
std::cout << a << " " << b << " " << c << " " << st << std::endl;

return 0;
}
'std::ws' does not set some kind of "from now on consider whitespace
not whitespace any longer" flag. No such flag exists. 'std::ws' just
skips all whitespace from current position to the next non-whitespace
character.

What you need is "get the rest of the stream into my 'string'" action.
Instead of 'is >st', use 'getline':

is >std::ws;
std::getline(is, st); // not the shift.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 13 '07 #2

P: n/a
mathieu wrote:
Hi,

I am having a hard time understanding how to use std::ws. I am
trying to parse a simple string (*). My goal would be that 'st'
contains 'Hello World'. I thought that using std::ws would indeed do
what I was looking for.
istream >string

Reads until it finds whitespace. Unfortunately, the space between the words
is whitespace. You could alter this behaviour by changing the locale, but
the easiest solution is to use the getline function.
#include <iostream>
#include <sstream>
#include <string>
#include <istream>

Formally, you need these two headers. Though most implementations of
<sstreamwill pull them in, you are not guaranteed this.
int main(int, char *[])
{
const char s[] = "1 2 3 Hello World";
std::istringstream is(s);
int a,b,c;
std::string st;
is >a;
is >b;
is >c;
is >std::ws;
is >st;
Change this last line to
std::getline(is, st);
std::cout << a << " " << b << " " << c << " " << st << std::endl;

return 0;
}
--
rbh
Jul 13 '07 #3

P: n/a
On 2007-07-13 16:31, mathieu wrote:
Hi,

I am having a hard time understanding how to use std::ws. I am
trying to parse a simple string (*). My goal would be that 'st'
contains 'Hello World'. I thought that using std::ws would indeed do
what I was looking for.

Thanks for suggestion,
-Mathieu

(*)
#include <iostream>
#include <sstream>

int main(int, char *[])
{
const char s[] = "1 2 3 Hello World";
std::istringstream is(s);
int a,b,c;
std::string st;
is >a;
is >b;
is >c;
is >std::ws;
is >st;
std::cout << a << " " << b << " " << c << " " << st << std::endl;

return 0;
}
No, std::ws will only extract the whitespace between the '3' and the
'Hello', but since you have not done 'is >std::noskipws' it will be
skipped anyway (so 'is >std::ws' will have no effect). I'm not sure
how to solve your problem but it might be possible to change the
delimiting characters somehow. Or if you know that the string will be at
the end use is.str().

--
Erik Wikström
Jul 13 '07 #4

P: n/a

Erik Wikström <Er***********@telia.comwrote in message...
On 2007-07-13 16:31, mathieu wrote:
Hi,
#include <iostream>
#include <sstream>

int main(int, char *[]){
// or just: 'int main(){}'
const char s[] = "1 2 3 Hello World";
std::istringstream is(s);
int a,b,c;
std::string st;
is >a;
is >b;
is >c;
is >std::ws;
is >st;
std::cout << a << " " << b << " " << c << " " << st << std::endl;
return 0;
}

No, std::ws will only extract the whitespace between the '3' and the
'Hello', but since you have not done 'is >std::noskipws' it will be
skipped anyway (so 'is >std::ws' will have no effect). I'm not sure
how to solve your problem but it might be possible to change the
delimiting characters somehow.
Or if you know that the string will be at the end use is.str().
Oh?

// ....
is >a >b >c;
is >std::ws; // could be done with 'is.ignore( 1 );'
// is >st;
st = is.str();
std::cout << a << " " << b << " " << c << " " << st << std::endl;
// out:1 2 3 1 2 3 Hello World

std::getline( is, st );
std::cout << a << " " << b << " " << c << " " << st << std::endl;
// out:1 2 3 Hello World
// ....

OP: If you only wanted to extract 'Hello' using std::getline(), you can set
the delimiter ('\n' is default).

// is >st;
std::getline( is, st, ' ' ); // note: that's a single char, not " "
(string).

// (should) out:1 2 3 Hello

--
Bob R
POVrookie
Jul 13 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.