[bug] istream::getline(char*,streamsize) still broken

Benjamin Kosnik bkoz@redhat.com
Sat Jul 22 12:09:00 GMT 2000


Brent:

I'm ready to concede. I've looked at existing practice and scoped the
reflector archives. It was my contention that after n - 1 characters
were extracted (ie, after 'd' in "abcd") that no more characters were
extracted, period. 

We are/were both in agreement about what gcount() returns.

This seems to be wrong. Here's what should happen (I'm hoping we can
now easily agree on this:)

#include <cstring>
#include <istream>
#include <sstream>

// http://sources.redhat.com/ml/libstdc++/2000-07/msg00126.html
int
main()
{
  using namespace std;

  bool test = true;
  const streamsize it = 5;
  char tmp[it];
  const char* str_lit = "abcd\n";

  stringbuf strbuf(str_lit, std::ios_base::in);
  istream istr(&strbuf);
  
  istr.getline(tmp,it); 
  test &= istr.gcount() == it;  // extracted whole string
  test &= strlen(tmp) == 4;     // stored all but '\n'
  test &= !istr.eof();          // extracted up to but not eof
  test &= !istr.fail(); 	// failbit not set
  
  char c = 'z';
  istr.get(c);
  test &= c == 'z';
  test &= istr.eof();

  return 0;
}



If you can re-submit your patch reflective of this, patch up any
testcase weirdness and add the above, that would be cool.

thanks,
benjamin


More information about the Libstdc++ mailing list