流的使用 [英] usage of ofstream

查看:82
本文介绍了流的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点被ofstream弄糊涂了. 从ostream继承的ofstream.而且它 也继承了方法"operator<<"从 ostream.

I'm kind of confused by ofstream. ofstream inherited from ostream. And it also inherited method "operator<<" from ostream.

    ofstream x;
    x << "hello world" << endl;
     //cout << "hello world" << endl;

    system("pause");
    return 0;

上面的代码片段正在尝试使用ofsream对象 像cout一样向终端输出"hello world".

The above code clip is trying to use an object of ofsream to output "hello world" to the terminal just as cout did.

上面的代码片段可以编译,但是什么也没显示. 为什么会发生?

The above code clip can compile but shows nothing. Why does it happen?

谢谢

推荐答案

已经很长时间了,但是流的IIRC是一个output_file-stream,它将数据流式传输到打开的文件中.为了使ofstream对象实际打印到终端,您必须使其打开"/dev/console"或类似的东西.一个普通的ofstream实例可能无法打开/dev/console b/c,而您已经有cout可用.

It's been a long time, but IIRC of stream is an output_file-stream which streams data into an opened file. For an ofstream object to actually print to the terminal you would have to make it open "/dev/console" or something similar. A plain instance of ofstream probably doesnt open /dev/console b/c you already have cout available.

这篇关于流的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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