libstdc++/9424: i/ostream::operator>>/<<(streambuf*) drops characters

Nathan Myers ncm-nospam@cantrip.org
Fri Mar 7 23:58:00 GMT 2003


On Fri, Mar 07, 2003 at 10:25:52PM +0100, Paolo Carlini wrote:
> Nathan Myers wrote:
> 
> >>int_type c;
> >>while ((c = sbin->sbumpc()) != traits_type::eof() &&
> >>      (sbout->sputc(c) != traits_type::eof())
> >> {}
>
> ... in the application I want to count separately the number of 
> succesful reads (__charsread) and writes (__xtrct) like this:
> 
>    __xtrct = __charsread = 0;
>    int_type __c = __sbin->sgetc();
>    while (!_Traits::eq_int_type(__c, _Traits::eof()))
>      {
>        ++__charsread;
>        if (_Traits::eq_int_type(__sbout->sputc(_Traits::to_char_type(__c)),
>                                 _Traits::eof()))
>          break;
>        ++__xtrct;
>        ++__ret;
>        __c = __sbin->snextc();
>      }
> 
> and can't see a straightforward way to use your suggestion. Can you?

Here's my best.

  __xtrct = __charsread = 0;
  while (true)
    {
      int_type __c = __sbin->sbumpc();
      if (_Traits::eq_int_type(__c ), _Traits_type::eof())
      	break;
      ++__charsread;
      __c = __sbout->sputc(__c);
      if (_Traits::eq_int_type(__c, _Traits_type::eof()))
      	break;
      ++xtrct;
      ++ret;
    }

Not so elegant, perhaps, but clear.  

Nathan Myers
ncm-nospam@cantrip.org



More information about the Libstdc++ mailing list