(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/