为什么在 c++ 中使用 ios::sync_with_stdio(false) 后在 cout 之前执行 printf
Why printf is executed before cout after using ios::sync_with_stdio(false) in c++?
#include <iostream>
#include <stdio.h>
int main () {
std::ios::sync_with_stdio(false);
std::cout << "hi from c++n";
printf("hi from cn");
return 0;
}
删除 std::endl 并将 放在 cout 语句中后,输出更改为以下内容:
hi from c
hi from c++
这是一个缓冲问题。
默认情况下,当标准输出连接到终端时,stdout
是行缓冲的,这意味着缓冲区被刷新,输出实际上以换行符写入终端。
当 C stdio 与C++标准流断开连接时,std::cout
是完全缓冲的,这意味着输出实际上是在显式刷新时写入的(例如使用std::flush
或std::endl
操纵器),或者缓冲区是否已满。
Cstdout
和C++std::cout
使用的两个缓冲区不同且未连接。
当程序退出时,也会刷新缓冲区。
程序中发生的情况是,由于字符串中的尾随换行符,带有printf
的输出会立即刷新。但是,只有当程序退出时,才会刷新std::cout
的输出。
相关文章:
- 在由Sublime文本3编译后在cmd上显示Java程序输出
- 对象初始化后在C++中显示 char 数组时的异常行为
- 如何在使用Makefile构建后在cmd中运行该程序?
- 如何在使用 getch 后在另一个字符串的末尾连接一个字符串
- 在c++中擦除后在映射上迭代时出现的奇怪问题
- 如何在每次迭代后在循环中获取变量的值?
- 为什么在 c++ 中使用 ios::sync_with_stdio(false) 后在 cout 之前执行 printf
- C++11:如何在 n 秒后在主线程中执行函数
- 在复制 delete[] 数组后在 C++ 中检测到堆损坏
- 防止线程在处理异常后在分离时调用 std::terminate()
- 线程完成后在另一线程中调用方法
- 为什么关闭初始程序后在ncat上看不到UDP?
- 如何在从用户获取输入后在函数中使用常量值
- 在C 中,如何在x秒后在类中重置变量
- CloseHandle 在 STDOUT 重定向后在 Studio 2010 中引发异常
- 如何在关机调用后在 UDP recvfrom() 调用中获取返回值 0
- ios_base::sync_with_stdio(false) 在来自标准输入的两个输入之间不起作用
- 无法理解为什么在添加数字后在末尾添加零
- EVR在播放前固定后在窗口上显示几个黑色框架
- 你需要在返回false后中断吗