flush and sync

Jul 24, 2013 at 10:11pm
What's the diferençe between flush and sync()?
If with flush i already make a synchronization, why exist sync or vice-versa?
Jul 24, 2013 at 11:22pm
Flush forces application buffered data to be written to the file system.

Sync forces the file system to flush its buffers to disk.
Jul 25, 2013 at 4:45pm
So i always need use the two of them?
Jul 25, 2013 at 4:59pm
Assuming that we are talking about C++ input and output streams:

flush() is used with output streams. http://en.cppreference.com/w/cpp/io/basic_ostream/flush

sync() is a member of an input stream. Its behaviour is is implementation-defined.
http://en.cppreference.com/w/cpp/io/basic_istream/sync
Jul 26, 2013 at 10:16pm
But when I close the file or the buffer is full, the associated stream buffer is automatically synchronized, so why would I use the flush?
Would any example where it is advantageous to use the flush or endl?
Jul 26, 2013 at 10:29pm
One place Windows users are well familiar with is just before system("pause");, if you don't flush/endl the last output before that statement, "Press ENTER to continue" will be printed by pause.exe, while your output would still be waiting for the flush.
Jul 27, 2013 at 4:44am
In general, flushing an output stream is expensive; and we would want to avoid fortuitous flushing (for instance with wanton use of std::endl).

There are a few cases where flushing a stream is a good idea.

A typical scenario is when we tie() two streams - we have an input stream and and output stream associated with the same physical device, and we need to synchronize the two stream buffers. Or we are appending to the same physical file via two different output steams. http://stdcxx.apache.org/doc/stdlibug/35-4.html

Another is when we want to create an audit trail, which must be complete and consistent even if our program terminates abnormally. (Typically, we use the format flag std::ios_base::unitbuf. See http://stdcxx.apache.org/doc/stdlibug/35-3.html)
Last edited on Jul 27, 2013 at 4:47am
Jul 27, 2013 at 6:23pm
" we have an input stream and output stream associated with the same physical device, and we need to synchronize the two stream buffers".
This happen when i use fstream with default constructor,right?

Like:
1
2
fstream file;
    file.open("file.ext",iso::in|ios::out)

Jul 27, 2013 at 8:12pm
No; it happens when you use two different stream objects. Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <fstream>
#include <iostream>

void test_it( std::istream& in, std::ostream& out )
{
    for( int i = 0 ; i < 5 ; ++i )
    {
        out << i << ' ' ;
        int v ;
        if( in >> v ) std::cout << v << ' ' ;
        else { std::cout << "fail " ; in.clear() ; }
    }
    std::cout << '\n' ;
}

int main()
{
    const std::string path = "file.txt" ;

    std::ofstream output(path) ;
    std::ifstream input(path) ;

    input.tie( &output ) ;
    std::cout << "tied: " ;
    test_it( input, output ) ;

    input.tie(nullptr) ;
    std::cout << "not tied: " ;
    test_it( input, output ) ;
}
Jul 28, 2013 at 6:14pm
Instead of using this tie (), I could use flush, right?
Jul 28, 2013 at 6:42pm
Yes.

tie() causes flush() to be called on the tied output stream prior to any input or output operation on the stream that has tied it.
Jul 29, 2013 at 6:26am
Thank you very much!
Topic archived. No new replies allowed.