Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Peek non-whitespace characters from a stream

149 views
Skip to first unread message

Daniel Lidström

unread,
Nov 3, 2004, 10:26:39 AM11/3/04
to
Hi,

how can I peek the next non-whitespace character from a std::istream? My
ugly solution is this:

char c;
stream >> c;
stream.unget();

--
Daniel

Igor Tandetnik

unread,
Nov 3, 2004, 11:12:38 AM11/3/04
to
"Daniel Lidström" <som...@microsoft.com> wrote in message
news:1j4svv0zg2kww$.12vvu9avhp06n$.d...@40tude.net

Try

stream >> skipws;
int ch = stream.peek();

--
With best wishes,
Igor Tandetnik

"On two occasions, I have been asked [by members of Parliament], 'Pray,
Mr. Babbage, if you put into the machine wrong figures, will the right
answers come out?' I am not able to rightly apprehend the kind of
confusion of ideas that could provoke such a question." -- Charles
Babbage


Igor Tandetnik

unread,
Nov 3, 2004, 11:17:01 AM11/3/04
to
"Igor Tandetnik" <itand...@mvps.org> wrote in message
news:eBuCh$bwEHA...@TK2MSFTNGP09.phx.gbl

> "Daniel Lidström" <som...@microsoft.com> wrote in message
> news:1j4svv0zg2kww$.12vvu9avhp06n$.d...@40tude.net
>> how can I peek the next non-whitespace character from a std::istream?
>> My ugly solution is this:
>>
>> char c;
>> stream >> c;
>> stream.unget();
>
> Try
>
> stream >> skipws;
> int ch = stream.peek();

Make it

stream >> ws;

Daniel Lidström

unread,
Nov 4, 2004, 2:23:21 AM11/4/04
to
On Wed, 3 Nov 2004 11:17:01 -0500, Igor Tandetnik wrote:

> "Igor Tandetnik" <itand...@mvps.org> wrote in message
>

> stream >> ws;
> int ch = stream.peek();

Thanks!

--
Daniel

Daniel Lidström

unread,
Nov 4, 2004, 3:18:10 AM11/4/04
to
On Wed, 3 Nov 2004 11:17:01 -0500, Igor Tandetnik wrote:

> "Igor Tandetnik" <itand...@mvps.org> wrote in message
>

> stream >> ws;
> int ch = stream.peek();

Why do I have to re-set the stream to skip whitespace? I would like to be
able to do it once. Surely there must be simple way to permanently set a
stream to skip whitespace?

--
Daniel

Igor Tandetnik

unread,
Nov 4, 2004, 9:55:23 AM11/4/04
to
"Daniel Lidström" <som...@microsoft.com> wrote in message
news:l0l678h76ri8$.z69uzckbt2zr$.d...@40tude.net

There is - see skipws and noskipws. However, this flag only works for
formatted input - basically, when you use operator>>. Unformatted input
methods (such as read() and peek() ) always reads raw elements with no
preprocessing.

0 new messages