C ++ / fstream:无法创建新文件

时间:2016-09-19 07:26:51

标签: c++ file fstream

由于某种原因,以下代码无法创建新文件,即如果文件已存在则会覆盖它,否则它什么也不做:

#include <fstream>

int main()
{
    std::fstream fs("test_out.log"); // <-- does not create a new file!
    //std::fstream fs("test_out.log", std::ios_base::in | std::ios_base::out); // <-- does not create a new file!

    fs << "TEST" << std::endl;
}

我做错了什么?

C ++编译器:clang
系统:OS-X 10.9.5

1 个答案:

答案 0 :(得分:2)

如果要创建要写入的新文件:

std::ofstream fs("test_out.log");

做好这份工作。

如果您使用std::ios_base::in文件必须存在