Pietro
Pietro

Reputation: 13150

C++/fstream: cannot create a new file

For some reason, the following code cannot create a new file, i.e. if the file already exists it overwrites it, otherwise it does nothing at all:

#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;
}

What have I done wrong?

C++ compiler: clang
System: OS-X 10.9.5

Upvotes: 2

Views: 258

Answers (1)

Klaus
Klaus

Reputation: 25593

If you want to create a new file to write:

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

do the job.

If you use std::ios_base::in the file MUST exist

Upvotes: 2

Related Questions