flush and sync

What's the diferen├že between flush and sync()?
If with flush i already make a synchronization, why exist sync or vice-versa?
Flush forces application buffered data to be written to the file system.

Sync forces the file system to flush its buffers to disk.
So i always need use the two of them?
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
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?
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.
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
" 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)

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 ) ;
}
Instead of using this tie (), I could use flush, right?
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.
Thank you very much!
Topic archived. No new replies allowed.