Can I flush data implicitly?

老子叫甜甜 提交于 2019-12-07 18:56:46

问题


Is there a way to implicitly flush data to an output stream?

#include <iostream>
#include <fstream>
using namespace std;

#define log logstream 

int main()
{
  ofstream logstream("test.log");

  log << "Test1" << 123 << endl;     // explicitly flushed
  log << "Test2" << 123;             // ?

  // Test2 not written, yet...

  cout << "Check log file..." << endl;
  int tmp;
  cin >> tmp;
}

I would like to be able to log without specifying the << endl manipulator every time.


回答1:


You may use std::unitbuf.

log << std::unitbuf;

And then flush would be done at each insertion.



来源:https://stackoverflow.com/questions/39645412/can-i-flush-data-implicitly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!