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

istream, stringstream & \n processing problem

0 views
Skip to first unread message

Stéphane Bailliez

unread,
Oct 5, 1999, 3:00:00 AM10/5/99
to
I have a slight problem here, I think I'm not doing it the good way so any
tip will be appreciated.

I have a string std::string mystring contening various chars for
example("\nabcdef\n12345\a1b2b3\n"), as you notice there are '\n'
characters.
I would like to get each char of the string using a stream. So I used
std::stringstream.
My problem is that the implementation of istream does the following call
(VC6SP3 STL):

while (!_Tr::eq_int_type(_Tr::eof(), _C) && _Fac.is(_Ctype::space,
_Tr::to_char_type(_C)))
_C = rdbuf()->snextc();

which is pretty annoying since in the Ctype table, the character '\n'(0x10)
is represented as a space (0x20)..and thus each '\n' in the string is
ignored as you can see in the while loop.
That is the following loop will never process the eol char.

std::string str("\nabcdef\n12345\a1b2b3\n");
std::stringstream ss(str);
while (ss.rdbuf()->in_avail())
{
char c;
ss>>c;
}

The only way I can see right now to process the \n char would be to use the
ss.rdbuf()->snextc() (basic_streambuf) method but I would really like to use
the >> operator of the stream.

Thanks for any info


--
Stéphane Bailliez, Paris - France
mailto:bail...@cybercable.fr

[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]


Dietmar Kuehl

unread,
Oct 6, 1999, 3:00:00 AM10/6/99
to
Hi,
In article <7tb8j0$bcp$1...@oceanite.cybercable.fr>,

"Stéphane Bailliez" <bail...@cybercable.fr> wrote:
> My problem is that the implementation of istream does the following
call
> (VC6SP3 STL):
>
> while (!_Tr::eq_int_type(_Tr::eof(), _C) &&
_Fac.is(_Ctype::space,
> _Tr::to_char_type(_C)))
> _C = rdbuf()->snextc();

This looks like a portion of the 'basic_istream<...>::sentry' ctor
alhtough I think that it is possible to come up with a more efficient
implementation (but my own implementation basically does the same).
The interesting part is that this code is only executed if the second
(the Boolean) argument to this constructor is 'false' and if the
'skipws' flag in 'flags()' applied to the first argument is set, ie.
this code is only executed if something like the following expression
yields 'true' (this is taken from my implementation):

!_CS_noskipws && (_CS_is.flags() & ios_base::skipws != 0)

'_CS_noskipws' is the second argument and you cannot change this
argument: It is set to 'false' in the formatted input functions.
'_CS_is' is the first argument, ie. the input stream, and you can change
the value returned from 'flags()' using the 'setf()' method or using an
appropriate manipulator. Thus, clearing the 'ios_base::skipws' flag will
prevent skipping of [leading] whitespace when constructing the 'sentry'
object.

Of course, this description is about what the standard says, not about
the implementation from Dinkumware. However, I'm pretty sure that they
got this right...

> The only way I can see right now to process the \n char would be to
> use the
> ss.rdbuf()->snextc() (basic_streambuf) method but I would really like
> to use
> the >> operator of the stream.

If you just want to read all characters from a stream, I recomment using
'std::istreambuf_iterator':

copy(std::istreambuf_iterator<char>(std::cin),
std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>(std::cout));

Using 'istreambuf_iterator' avoids some overhead involved when using
the input operators (more precisely the construction of the 'sentry'
object) which is not used anyway. The interface to iterators in C++ is
well known and thus preferable to the interface of 'streambuf'. However,
if you really need high performance, you might be best off using indeed
'snextc()'...
--
<mailto:dietma...@claas-solutions.de>
homepage: <http://www.informatik.uni-konstanz.de/~kuehl>


Sent via Deja.com http://www.deja.com/
Before you buy.

0 new messages