newbie questions - cout and stdout

Discussion in 'C++' started by nospam, Aug 17, 2005.

  1. nospam

    nospam Guest

    The following expression:

    cout << "hello" << "\n";

    has two properties that confuse me.

    First, on the system I'm on, text file lines are terminated by newline and
    carriage return (both). Why does "\n" start a new line on my system? I
    suppose I could ask the same question about printf() on comp.lang.c. Isn't
    line termination system dependent?

    Second, my book says, very clearly:

    There are three events that cause the system to flush the buffer.
    First, the buffer might be full ... Second, the library may be
    asked to read from stdin stream ... Third, when we explicitly say
    to do so.

    None of the three conditions seem to exist in the expression above. Clearly,
    the buffer is not less than 5 characters wide. Not even 5 wide characters!
    ;) I'm NOT asking to read from stdin. Lastly, I didnt' explicitly say to
    flush the buffer.

    So exactly why does the expression above seem to flush the stdout buffer?

    Thanks!
    Pete
     
    nospam, Aug 17, 2005
    #1
    1. Advertising

  2. nospam wrote:
    > The following expression:
    >
    > cout << "hello" << "\n";
    >
    > has two properties that confuse me.
    >
    > First, on the system I'm on, text file lines are terminated by newline and
    > carriage return (both). Why does "\n" start a new line on my system? I
    > suppose I could ask the same question about printf() on comp.lang.c. Isn't
    > line termination system dependent?


    The single \n character when output is probably converted into \r\n (or
    \n\r, pick one). That's taken care of the system library.

    > Second, my book says, very clearly:
    >
    > There are three events that cause the system to flush the buffer.
    > First, the buffer might be full ... Second, the library may be
    > asked to read from stdin stream ... Third, when we explicitly say
    > to do so.


    I think the #2 is only true when the input and output are synchronized.

    > None of the three conditions seem to exist in the expression above. Clearly,
    > the buffer is not less than 5 characters wide. Not even 5 wide characters!
    > ;) I'm NOT asking to read from stdin. Lastly, I didnt' explicitly say to
    > flush the buffer.
    >
    > So exactly why does the expression above seem to flush the stdout buffer?


    Whatever your book says doesn't have to be the limited set. The book does
    not say "There are _only_ three events that ...", does it? There can be
    other events that flush the buffer. On your system, that is. Possibly
    the output of \n can trigger flushing, right?

    V
     
    Victor Bazarov, Aug 17, 2005
    #2
    1. Advertising

  3. nospam

    benben Guest

    > Whatever your book says doesn't have to be the limited set. The book does
    > not say "There are _only_ three events that ...", does it? There can be
    > other events that flush the buffer. On your system, that is. Possibly
    > the output of \n can trigger flushing, right?
    >
    > V


    Or a flush call from destructor...
     
    benben, Aug 18, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. abi

    man cout or info cout

    abi, Jun 27, 2003, in forum: C++
    Replies:
    2
    Views:
    1,672
  2. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,447
    Leor Zolman
    Jun 2, 2004
  3. Replies:
    4
    Views:
    342
    Howard
    Sep 15, 2005
  4. Stefan Ram

    Re: cout vs std::cout

    Stefan Ram, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    512
  5. Hendrik Schober

    Re: cout vs std::cout

    Hendrik Schober, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    463
    Jerry Coffin
    Oct 7, 2008
Loading...

Share This Page