Pietro Pietro - 1 year ago 66
C++ Question

Can I flush data implicitly?

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.

Answer Source

You may use std::unitbuf.

log << std::unitbuf;

And then flush would be done at each insertion.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download