dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: At what time i should use cout.flush()

  1. #1
    Join Date
    Nov 2004
    Posts
    92

    At what time i should use cout.flush()

    my respected gurus:
    I am reading some source code, in them the statement cout.flush() appears several times. In the book, I find such explanation "cout.flush() will puts all the pending characters that have been stored in the internal stream buffer but not yet output" I don't understand this sentence, can somebody explain this for me, and tell me at what situation i should use cout.flush()??
    Thanks very much!
    If u answer my question, I will surely give reputation to you untill i have no reputation! But i will try to get more reputation, and give it to you lately!

  2. #2
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: At what time i should use cout.flush()

    An interesting point to note here is that whenever u use the "endl" statement while cout etc it automatically causes the flush(). The endl manipulator has a special behavior when it is used with buffered streams: they are flushed. But anyway cout is unbuffered by default.

    Now back to ur questions - lets see what this does - sometimes when a physical device is associated with a stream like say files which are slower than the direct memory access, buffered streams are used. These utilize the memory and to synchronize whats happening there with the physical devices we need to call the flush () operation. flush() causes a write to output to occur immediately.

    Hope I am making sense over here.

    Cheers,
    Exterminator
    Last edited by exterminator; August 5th, 2005 at 05:58 AM.

  3. #3
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: At what time i should use cout.flush()

    If you write to the console or a file (among others) the "underlying system' may store the data in a internal buffer and wait to do the actual write operstion. This is to reducde the cost of multiple write operations.

    For instance if you do this :

    Code:
    loop 100 times
         write 2 byte to a file
    end loop
    then the underlying file system may choose to store 100 bytes in the internal buffer and thus your're ending up with just 2 write operations to the file:

    write 100 bytes
    write 100 bytes


    But if your doing this :
    Code:
    loop 100 times
         write 2 byte to a file
         flush
    end loop
    then the file is actually written to 100 times.

    As exterminator says, "flush() causes a write to output to occur immediately", and ensures that there are no bytes left in the internal buffer.

    - petter

  4. #4
    Join Date
    Nov 2004
    Posts
    92

    Re: At what time i should use cout.flush()

    Quote Originally Posted by exterminator
    An interesting point to note here is that whenever u use the "endl" statement while cout etc it automatically causes the flush().

    Now back to ur questions - lets see what this does - sometimes when a physical device is associated with a stream like say files which are slower than the direct memory access, buffered streams are used. These utilize the memory and to synchronize whats happening there with the physical devices we need to call the flush () operation. flush() causes a write to output to occur immediately.

    Hope I am making sense over here.

    Cheers,
    Exterminator
    Hello,exterminator, after wildfrog's explain, i understand the function of flush. But i am not sure about your explanation about the situation that needs flush, can you have a look over my understanding?
    The following is my understanding:
    1." sometimes when a physical device is associated with a stream like say files which are slower than the direct memory access," for example there are a signal generator a siganal acquire machine, and a computer !In Every second the signal acquire machine will accept the 1000 signals from the signal generators, and then transfert the data to the computer and the computer will write the signals to hardisk! Because the speed of the computer is much higher than the signal acquire machine, so if we just write statements like
    Code:
    while(signal_acquire_mahcine_getting_signal==successful)
    {
            signal_to_computer=signal_acquire_mahcine_getting_signal;
            writing  signal_to_computer to hardisk
    
    }
    than maybe the computer will first read 500 signals in the internal buffer, then write 500 signals to hardisk! So there are altogether 2 writing process!
    There are two events: the event that signal_acquire_mahcine read the signal and the event that the computer write the signal to the hardisk !
    These two events are asynchronous!

    however if i wirte statements like following
    Code:
    while(signal_acquire_mahcine_getting_signal==successful)
    {
            signal_to_computer=signal_acquire_mahcine_getting_signal;
            writing  signal_to_computer to hardisk
            flush
    }
    then the two events are synchronous!!!


    Am I right?
    If u answer my question, I will surely give reputation to you untill i have no reputation! But i will try to get more reputation, and give it to you lately!

  5. #5
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: At what time i should use cout.flush()

    Yes this seems to be a good analogy.

    Cheers,
    Exterminator.

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: At what time i should use cout.flush()

    I know this thread has been long dead but I recently read about buffers and synchronization here and came to know more about this. Calling flush is one way of causing buffer synchronization. I would qoute the content as:
    Buffers and Synchronization
    When we operate with file streams, these are associated to a buffer of type streambuf. This buffer is a memory block that acts as an intermediary between the stream and the physical file. For example, with an out stream, each time the member function put (write a single character) is called, the character is not written directly to the physical file with which the stream is associated. Instead of that, the character is inserted in the buffer for that stream.
    When the buffer is flushed, all data that it contains is written to the physic media (if it is an out stream) or simply erased (if it is an in stream). This process is called synchronization and it takes place under any of the following circumstances:

    1. When the file is closed: before closing a file all buffers that have not yet been completely written or read are synchronized.
    2. When the buffer is full: Buffers have a certain size. When the buffer is full it is automatically synchronized.
    3. Explicitly with manipulators: When certain manipulators are used on streams a synchronization takes place. These manipulators are: flush and endl.
    4. Explicitly with function sync(): Calling member function sync() (no parameters) causes an immediate syncronization. This function returns an int value equal to -1 if the stream has no associated buffer or in case of failure.

  7. #7
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Lightbulb Re: At what time i should use cout.flush()

    Quote Originally Posted by walkinginwater
    my respected gurus:
    I am reading some source code, in them the statement cout.flush() appears several times. In the book, I find such explanation "cout.flush() will puts all the pending characters that have been stored in the internal stream buffer but not yet output" I don't understand this sentence, can somebody explain this for me, and tell me at what situation i should use cout.flush()??
    Thanks very much!
    I think you didn't check MSDN any way here is some stuff.you can found in MSDN also
    Output Stream cout Is Buffered
    Code:
    In an application developed with Microsoft C/C++, the cout stream is 
    buffered. In other words, information sent to the cout stream does not 
    appear on the screen until its buffers are flushed. For Visual C++ versions 
    4.2 and 5.0, this behavior only occurs when using the old iostream libraries. 
    There are four methods to flush the cout buffer, as follows: 
    Use the endl manipulator to insert a newline character into the output 
    stream and flush the buffer. Use the insertion operator with the endl 
    manipulator, as follows: 
    cout << ... << endl;
    						
    Use the flush member function in the ostream class or the flush 
    manipulator. The flush manipulator does not insert a newline character into 
    the stream before it flushes the buffer. To call the flush member function, 
    use code similar to the following: 
    
    cout.flush();
    
    Use the insertion operator with the flush manipulator as follows: 
    cout << ... << flush;
    						
    
    Read from the cin stream or write to the cerr or clog streams. Because 
    these objects share the buffer with cout, each flushes the contents of the 
    buffer before making any changes to it. 
    Exit the program to flush all buffers currently in use.

  8. #8
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: At what time i should use cout.flush()

    Quote Originally Posted by humptydumpty
    Output Stream cout Is Buffered
    Actually, you can control whether or not cout (or any i/o stream, for that matter) is buffered or not using the ios_base::unitbuf flag.

    Code:
    cout.setf(ios_base::unitbuf);      // cout is now unbuffered
    cout.unsetf(ios_base::unitbuf);  // cout is now buffered
    As exterminator correctly pointed out, according to the Standard cout is unbuffered by default.
    Old Unix programmers never die, they just mv to /dev/null

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)