🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.lang.c++
Article #115

Question about stream I/O buffering

#115
From: Jonathan P. Leech
Date: Tue, 25 Mar 1986 06:30
70 lines
2362 bytes
Summary:
Expires:
Sender:
Followup-To:

Organization : California Institute of Technology
Keywords:

    The C++ book claims (sec 8.4.4; page 240) that

   "...cin is tied to cout; this means that cin executes a
	cout.flush();	// write output buffer
    before attempting to read characters from its file."

    Playing with various forms of stream I/O leaves me	puzzled  about
this assertion. Consider the following example:

__@/ cat junk.c
#include <stream.h>
main()
{
    char buf[120];

    //	Test buffering of get(char *, int)
    //	Should read and echo two lines
    for (int i = 0; i < 2; i++) {
	cin.get(buf, 120);
	cout << "get(char *, int): " << buf << "\n";
    }

    //	Test buffering of get(char &)
    //	Should also read and echo two lines
    for (i = 0; i < 2; i++) {
	    int j = 0;
	    while (!cin.eof()) {
		cin.get(buf[j++]);
		if (buf[j-1] == '\n')
		    break;
	    }
	    buf[j - 1 + cin.eof()] = '\0';
	    cout << "get(char &): " << buf << "\n";
    }
}
__@/ junk
This is line 1	    < I type this
This is line 2	    < and this
This is line 3	    < and this (no EOF)
get(char *, int): This is line 1    < THEN, it prints out these lines
get(char *, int):
get(char &):
get(char &): This is line 2

    I expected something more like

This is line 1
get(char *, int): This is line 1
This is line 2
get(char *, int): This is line 2
This is line 3
get(char &): This is line 3
This is line 4
get(char &): This is line 4

    Can anyone tell me why cout is not buffered as the book  seems  to
claim it should be, or why the middle two read operations got  nothing
at all?  I don't understand this at all.

    Thanks,
    Jon Leech (jon@csvax.caltech.edu || ...seismo!cit-vax!jon)
    __@/
Technical Details
Message-ID: <292@cit-vax.Caltech.Edu>
Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!cit-vax!jon

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!cit-vax!jon
From: jon@cit-vax.Caltech.Edu (Jonathan P. Leech)
Newsgroups: net.lang.c++
Subject: Question about stream I/O buffering
Message-ID: <292@cit-vax.Caltech.Edu>
Date: Tue, 25-Mar-86 01:30:20 EST
Article-I.D.: cit-vax.292
Posted: Tue Mar 25 01:30:20 1986
Date-Received: Wed, 26-Mar-86 07:05:15 EST
Reply-To: jon@cit-vax.UUCP (Jonathan P. Leech)
Distribution: net
Organization: California Institute of Technology
Lines: 65