c++ - 在没有指向任何文件名的情况下使用 ofstream 时 << 发生了什么?

标签 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).

在没有指定任何文件名的情况下使用 ofstream 时 << 发生了什么?

ofstream  ofstream;
ofstream<<1<<endl;

“1”去哪儿了?有什么问题吗?我试过了,没问题。但我找不到任何代码线索,任何人都可以显示内部代码解释吗?

最佳答案

没有任何反应。

[C++11: 27.9.1.1/3]: In particular:

  • If the file is not open for reading the input sequence cannot be read.
  • If the file is not open for writing the output sequence cannot be written.
  • A joint file position is maintained for both the input sequence and the output sequence

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

例子:

#include <iostream>
#include <fstream>

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

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

// Output: 0

Live demo

关于c++ - 在没有指向任何文件名的情况下使用 ofstream 时 << 发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20941939/

相关文章:

C++ 缩进输出类继承 ofstream

c++ - Ofstream,使用变量命名

c++ - 写入数据时 std::ofstream 中的错误处理

c++ - OpenGL glTexParameteri错误0x0500

c++ - 快速预订问题

c++ - 是什么导致 WS_TABSTOP 影响窗口绘制顺序?

c++ - 运行时 ofstream 错误

C++ 成员函数作为全局函数的参数?

c++ - 类似于大量项目的双端队列,但少量的内存使用量很小?

c++ - 在 C++ 中关闭 ofstream 时出现段错误