Thread: Output stream help

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    5

    Output stream help

    Hi,

    I have implemented the virtual function of the streambuf class
    In specification is written that overflow() function is callled when buffer is full.
    but i have observed that this function ( overflow() ) is called on writing the each character in output stream e.g. on writing the abc , this function is called three times Why?????
    Is the specification wrong or default buffer size is one byte?


    Code:
    cout.rdbuf(&DebugStreamBuf);
    
    cout << abc << endl;
    
    int DebugStreamBuf::overflow(int ch)
    {
     ...
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It does seem like you need to set the buffer & size using setbuf().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    what is the default streambuf size? Would you please shortly describe the whole process.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by alibaba786 View Post
    what is the default streambuf size? Would you please shortly describe the whole process.
    I have never done that - I'm just guessing from the cppreference.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    matsp is right. You need to set a buffer if you don't want overflow() to be called for every character. How large the buffer is is up to you - but you have to manage it. I guess typical sizes will be about 4k.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    It means , if i dont set the buffer size , output stream consider the buffer size . am i right???
    Is it specified somewhere??

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You actually have to provide the buffer. The size just needs to match the buffer. This is documented in the C++ standard, in the description of basic_streambuf. Although, of course, in standardese.

    The best resource for using streams is "Standard C++ IOStreams and Locales", by Langer and Kreft, Addison Wesley.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    My question still the same, if i dont set the buffer size , what would be the buffer size?
    or by default output stream sets the buffer size = 1 byte???
    Is it specified somewhere??

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    If you don't set a buffer, there is no buffer, so the buffer size is 0. Thus, every character immediately overflows.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    Thanks . now i have got the answer ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange virtual function output
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2008, 08:08 AM
  2. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 06:41 PM
  3. 2d game
    By JordanCason in forum Game Programming
    Replies: 5
    Last Post: 12-08-2007, 10:08 PM
  4. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  5. sorting output on student info program
    By indigo0086 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2002, 11:29 AM