当没有任何文件名指向使用ofstream时,会发生什么?

jia*_*afu 1 c++ ofstream

(1) default constructor
Constructs an ofstream object that is not associated with any file.
Internally, its ostream base constructor is passed a pointer to a newly constructed filebuf object (the internal file stream buffer).
Run Code Online (Sandbox Code Playgroud)

当没有任何文件名指向使用ofstream时,会发生什么?

ofstream  ofstream;
ofstream<<1<<endl;
Run Code Online (Sandbox Code Playgroud)

"1"在哪里?有什么问题吗?我试了一下,没问题.但我找不到任何代码线索,有人可以显示内部代码解释吗?

Lig*_*ica 5

什么都没发生.

[C++11: 27.9.1.1/3]: 特别是:

  • 如果文件未打开,则无法读取输入序列.
  • 如果文件未打开以进行写入,则无法写入输出序列.
  • 为输入序列和输出序列保持联合文件位置

关闭流,设置错误标志并忽略数据.

例:

#include <iostream>
#include <fstream>

int main()
{
    std::ofstream ofs;
    ofs << 1 << std::endl;

    std::cout << ofs.good() << std::endl;
}

// Output: 0
Run Code Online (Sandbox Code Playgroud)

现场演示