473,473 Members | 2,304 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using std::ws

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
4 8247
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
10
by: William Payne | last post by:
Hello, when I was writing a user-driven test program for a data structure I wrote, I encountered an annoying problem. The test program is laid out as a menu with several numbered options. The user...
3
by: Abhishek | last post by:
Hi, I am trying to use the STL for C++ on Compaq. From what I read in the man pages and several online documentation, it is recommended that in order to use the STL on COMPAQ , I need to use the...
2
by: Don Wash | last post by:
Hi All! I've been searching everywhere for a simple sample of producing a bar graph using CrystalReport by specifying SQL Query, and I've found none of it! I find so many complex samples with so...
9
by: alopatenko | last post by:
I have a template class template <Class W> class WS At some point I have to use a STL list WS<W>objects so, I define #include <list>
10
by: Jeffrey Walton | last post by:
Hi All, I've done a little homework (I've read responses to similar from P.J. Plauger and Dietmar Kuehl), and wanted to verify with the Group. Below is what I am performing (Stroustrup's...
0
by: Basil | last post by:
Hello! Could You help me? Where can I get samples with using of WS-AtomicTransaction and WS-ReliableMessaging? Thanks, Basil.
6
by: frohlinger | last post by:
Hi, I need to perform some numeric calculations on a numeric float value, that is received as wstring. I would like to perform a check before converting the wstring to float, checking that indeed...
1
by: levent | last post by:
Hi All, Consider the following block of code: std::istringstream in( "1" ); // no space after `1' int i; in >i >std::ws; assert( !in::fail() ) Does the standard have any mention whether...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.