问题
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