getline() doesn't seem to work?

H.J. Lu hjl@lucon.org
Fri Jan 30 11:19:00 GMT 1998


> 
> Adrian Miranda <ade@psg.com> writes:
> 
> > I am using egcs 1.0.1 on HP-UX 10.20.
> > 
> > I don't know if this is a bug in getline, or just a bug in my
> > understanding of getline.  When I have the following program just
> > reading from my terminal, I have to hit control-D twice before the
> > loop exits.  It appears that the first time I hit control-D, getline
> > does not even return, it's only when I hit CONTROL-D a second time
> > that getline returns false, and the loop exits.
> > 
> > 
> > #include <iostream.h>
> > main() {
> >     char word[100];
> >     while(cin.getline(word, 100, '\n')) {
> >         cout << "Got another line" << endl;
> >     }
> > }
> 
> Same thing happens on my linux machine.
> 
> Joe Buehler
> 

How is this patch?


-- 
H.J. Lu (hjl@gnu.org)
---
Fri Jan 30 07:47:57 1998  H.J. Lu  (hjl@gnu.org)

	* isgetline.cc (istream::getline, istream::get): Call
	streambuf::in_avail () to check EOF when _IO_getline ()
	returns 0.

--- egcs-1.0.1/libio/isgetline.cc	Thu Aug 21 15:58:19 1997
+++ egcs/libio/isgetline.cc	Fri Jan 30 10:46:06 1998
@@ -39,7 +39,14 @@
     {
       streambuf *sb = rdbuf();
       _gcount = _IO_getline(sb, buf, len - 1, delim, -1);
-      ch = sb->sbumpc();
+      // We have to be vary careful here. _IO_getline () returns 0
+      // may indicate a delim is seen or EOF is seen. When EOF is
+      // seen, sb->in_avail () should not return a postive number.
+      // If we try to read again, we may not return immediately.
+      if (_gcount == 0 && sb->in_avail () <= 0)
+	ch = EOF;
+      else
+	ch = sb->sbumpc();
       if (ch == EOF)
 	set (_gcount == 0 ? (ios::failbit|ios::eofbit) : ios::eofbit);
       else if (ch != (unsigned char) delim)
@@ -68,7 +75,11 @@
     {
       streambuf *sbuf = rdbuf();
       long count = _IO_getline(sbuf, buf, len - 1, delim, -1);
-      if (count == 0 && sbuf->sgetc() == EOF)
+      // We have to be vary careful here. _IO_getline () returns 0
+      // may indicate a delim is seen or EOF is seen. When EOF is
+      // seen, sbuf->in_avail () should not return a postive number.
+      // If we try to read again, we may not return immediately.
+      if (count == 0 && sbuf->in_avail () <= 0)
 	set(ios::failbit|ios::eofbit);
       else
 	_gcount = count;



More information about the Gcc mailing list