【发布时间】:2016-09-05 07:41:37
【问题描述】:
fstream 与 oftream 之间的行为不同,我无法解释。
当我使用fstream 时,什么都没有发生,即没有文件被创建:
int main()
{
std::fstream file("myfile.txt");
file << "some text" << std::endl;
return 0;
}
但是当我将fstream 更改为oftream 时,它可以工作。
为什么?
fstream CTOR 的第二个参数是ios_base::openmode mode = ios_base::in | ios_base::out,这让我觉得文件是以读写模式打开的,对吧?
【问题讨论】:
-
应该可以。缓冲?我认为我们需要一个完整的minimal reproducible example。
-
我只有一个带有此代码的功能,但它不起作用。我没有什么可写的了。 MVS2015.
-
你检查它是否打开了吗?比如
if (!file) cout <<"Error";。你试过std::ofstream file(...吗? -
当然,您还有更多内容要写:这两行将无法编译。你至少需要一个头文件来定义 std::fstream 并且你需要一个
main函数。
标签: c++