std::ios_ba..._with_stdio(3)
okay bestie, welcome to the C++ Standard Libary manual page for std::ios_ba..._with_stdio(3) β aka std::ios_base::sync_with_stdio, the function that decides whether your C++ streams and C streams are synced up or doing their own separate thing. π
π NAME π
std::ios_base::sync_with_stdio β std::ios_base::sync_with_stdio
β¨ Synopsis β¨
static bool sync_with_stdio( bool sync = true );
Okay so this sets whether the standard C++ streams are synchronized to the standard C streams after each input/output operation. no cap, thatβs the whole vibe. β¨
The standard C++ streams are these eight: std::cin, std::cout, std::cerr, std::clog, std::wcin, std::wcout, std::wcerr and std::wclog.
The standard C streams are these three: stdin, stdout and stderr.
For a standard stream str, synchronized with the C stream f, the following pairs of functions have identical effect:
std::fputc(f, c)andstr.rdbuf()->sputc(c).std::fgetc(f)andstr.rdbuf()->sbumpc().std::ungetc(c, f)andstr.rdbuf()->sputbackc(c).
In practice, this means 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). periodt. π
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. That is a big yikes β do not play with it. π
π Parameters π
syncβ the new synchronization setting
π¦ Return value π¦
Synchronization state before the call to the function. Thatβs the tea. π«Ά
π Example π
Run this code:
// Run this code
#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:
b
a
c
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR:
LWG 49β Applied to:C++98- Behavior as published: it was unspecified (1) which state is actually returned and (2) what does 'synchronized' between standard C and C++ streams mean
- Correct behavior: both specified
π See also π
coutβ writes to the standard C output streamstdoutwcoutβ (global object)cerrβ writes to the standard C error streamstderr, unbufferedwcerrβ (global object)clogβ writes to the standard C error streamstderrwclogβ (global object)
mwah π β this pageβs source/version is http://cppreference.com, dated 2024.06.10, and the page title is std::ios_ba..._with_stdio(3). go off, bestie π
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms