Topic: istream, stringstream & \n processing problem
Author: "St phane Bailliez" <bailliez@cybercable.fr>
Date: 1999/10/05 Raw View
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:bailliez@cybercable.fr
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Dietmar Kuehl <dietmar.kuehl@claas-solutions.de>
Date: 1999/10/07 Raw View
In article <7tdd63$ov1$1@oceanite.cybercable.fr>,
"St phane Bailliez" <bailliez@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();
Just clear the 'ios_base::skipws' flag in the format flags of the
corresponding stream. In this case, the constructor of 'istream::sentry'
(this is where the code was apparently taken from) does not skip leading
white space. However, to read just characters, you might be better off
using 'istreambuf_iterator'.
For a longer answer, see my answer in comp.lang.c++.
--
<mailto:dietmar.kuehl@claas-solutions.de>
homepage: <http://www.informatik.uni-konstanz.de/~kuehl>
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]