我可以隐式刷新数据吗? [英] Can I flush data implicitly?

查看:94
本文介绍了我可以隐式刷新数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种隐式刷新数据到输出流的方法?

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;
}

我希望能够在不每次指定<< endl操纵器的情况下登录.

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

推荐答案

您可以使用 std::unitbuf .

You may use std::unitbuf.

log << std::unitbuf;

然后在每次插入时进行刷新.

And then flush would be done at each insertion.

这篇关于我可以隐式刷新数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆