| I/O manipulators | ||||
| Print functions(C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
| Synchronized Output | ||||
(C++20) | ||||
| Types | ||||
| Error category interface | ||||
(C++11) | ||||
(C++11) |
| Member functions | ||||
| Formatting | ||||
| Locales | ||||
| Internal extensible array | ||||
| Miscellaneous | ||||
ios_base::sync_with_stdio | ||||
| Member classes | ||||
| Member types | ||||
staticbool 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 andstd::wclog.
The standard C streams are the following:stdin,stdout andstderr.
For a standard streamstr, synchronized with the C streamf, the following pairs of functions have identical effect:
In practice, this means that the synchronized C++ streams are unbuffered, and each I/O operation on a C++ stream is immediately applied to the corresponding C stream's buffer. This makes it possible to freely mix C++ and C I/O.
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.
If this function is called after I/O has occurred on the standard stream, the behavior is implementation-defined: implementations range from no effect to destroying the read buffer.
Contents |
| sync | - | the new synchronization setting |
Synchronization state before the call to the function.
#include <cstdio>#include <iostream> int main(){ std::ios::sync_with_stdio(false);std::cout<<"a\n";std::printf("b\n");std::cout<<"c\n";}
Possible output:
bac
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 49 | C++98 | it was unspecified (1) which state is actually returned and (2) what does 'synchronized' between standard C and C++ streams mean | both specified |
| writes to the standard C output streamstdout (global object)[edit] | |
| writes to the standard C error streamstderr, unbuffered (global object)[edit] | |
| writes to the standard C error streamstderr (global object)[edit] |