< cpp‎ | io‎ | ios base
Revision as of 14:41, 10 March 2014 by Goto (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
static bool sync_with_stdio( bool sync = true );

Sets whether the standard C++ streams are synchronized to the standard C streams after each input/output operation.

The standard C++ streams are the following: std::cin, std::cout, std::cerr, std::clog, std::wcin, std::wcout, std::wcerr and std::wclog

The standard C streams are the following: stdin, stdout and stderr

For a standard stream str, synchronized with the C stream f, the following pairs of functions have identical effect:

1) std::fputc(f, c) and str.rdbuf()->sputc(c)
2) std::fgetc(f) and str.rdbuf()->sbumpc()
3) std::ungetc(c, f) and str.rdbuf()->sputbackc(c)

In practice, this means that the C++ and the C streams use the same buffer, and therefore, can be mixed freely. In addition, synchronized C++ streams are guaranteed to be thread-safe (individual characters output from multiple threads may interleave, but no data races occur)

If the synchronization is turned off, the C++ standard streams are allowed to buffer their I/O independently, which may be considerably faster in some cases.

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

It is implementation-defined if this function has any effect if called after some I/O occurred on the standard stream.


[edit] Parameters

sync - the new synchronization setting

[edit] Return value

synchronization state before the call to the function

[edit] Example

#include <iostream>
#include <cstdio>
int main()
    std::cout << "a\n";
    std::cout << "c\n";



[edit] See also

writes to the standard C output stream stdout
(global object) [edit]
writes to the standard C error stream stderr, unbuffered
(global object) [edit]
writes to the standard C error stream stderr
(global object) [edit]