如何将控制台指定为要使用ostream写入的文件? [英] How to specify the console as a file to write on with ostream?

查看:325
本文介绍了如何将控制台指定为要使用ostream写入的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cout ostream 类的对象.
另外,当我们写:

cout is an object of the ostream class.
Also, when we write:

filebuf   objFileBuf;
objFileBuf.open ("test.txt", ios :: out);

ostream objOstream (&objFileBuf);
objOstream << "Test sentence\n";

objFileBuf.close();

文本被写入文件.

如何将控制台"指定为要通过ostream写入的文件?

How to specify the "console" as a file to write through ostream?

推荐答案

您可以执行以下操作:

ostream objOstream (cout.rdbuf());
objOstream << "Test sentence goes to console\n";

或者您可以这样做:

ostream & objOstream = cout;
objOstream << "Test sentence goes to console\n";

第二个不适用于streambuf,而只是创建对std::cout的引用,并使用该引用.

The second one doesn't work with streambuf, rather you just create a reference to the std::cout, and use the reference.

这篇关于如何将控制台指定为要使用ostream写入的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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