Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is exact meaning of iostream is sync with ios_base::sync_with_stdio

Is this simply means whatever we do with object like cout gets in sync with stdout (and vice versa?). What does this exactly mean. Is stdio also sync with stdout?

like image 889
Pranit Kothari Avatar asked Jul 26 '13 03:07

Pranit Kothari


2 Answers

If the synchronization is off, the C++ streams will be faster in some cases.

By default, all standard C++ streams are synchronized with their respective C streams.

Example:

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    cout.sync_with_stdio(false);
    cout << "Hello\n";
    printf("World\n");
    cout << "...\n";
}

Output:

Hello
...
World

Changing it to true gives default result in order. Output:

Hello
World
...
like image 57
P0W Avatar answered Oct 10 '22 02:10

P0W


The default ostream used by std::cout and stdio(like printf) is stdout, but it is not necessarily so.

The output could alway be redirected to the other destination. Referencing this: http://www.tldp.org/LDP/abs/html/io-redirection.html

like image 45
lulyon Avatar answered Oct 10 '22 02:10

lulyon