Can I flush data implicitly?

2019-05-28 12:36发布

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条回答
趁早两清
2楼-- · 2019-05-28 12:46

You may use std::unitbuf.

log << std::unitbuf;

And then flush would be done at each insertion.

查看更多
登录 后发表回答