This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

bug in istream.get(b,c,d) ?


If a get(buffer, count, delim) call reads 0 characters
(because the next character on the stream is a delimiter),
then a subsequent call to get(void) returns EOF even though
there are more characters on the stream to be read.  I
don't have the C++ specification, so I'm not sure if this
is counter to spec, but it is definitely surprising behavior.

For example, this simple C++ version of cat(1) stops catting
when it gets to a blank line:

#include <iostream>
#include <fstream>

using namespace std;

int
main(int argc, char *argv[])
{
    if (argc != 2)
    {
        cerr << "Usage: " << argv[0] << " <file>" << endl;
        exit(1);
    }

    ifstream file(argv[1], ios::in);
    char l[255];

    for (;;)
    {
        file.get(l, 255,'\n');
        cout << "file.gcount() == " << file.gcount() << endl;
        int next = file.get();

        if (next == EOF)
            break;

        cout << l << (char)next;
    }
}

-- 
Mark REED 		     | CNN Internet Technology
1 CNN Center Rm SW0831G      | mark.reed@cnn.com
Atlanta, GA 30348      USA   | +1 404 827 4754 
--
	"Do you believe in intuition?"
	"No, but I have a strange feeling that someday I will."


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]