Armen Tsirunyan
Armen Tsirunyan

Reputation: 133024

How do I create a file with boost filesystem without opening it

In boost filesystem there is a function create_directory which creates a directory. How do I create a file? I could create one by defining a boost::filesystem::ofstream object but that would also open the file, so I would have to call close on it before I could do other stuff to it, like renaming or deleting. Is this the only way?

Upvotes: 10

Views: 20220

Answers (2)

Christian Severin
Christian Severin

Reputation: 1831

You could just use something like

    // ... code ...
    boost::filesystem::ofstream( "/path/to/file" );
    boost::filesystem::rename( "/path/to/file", "/path/to/renamed_file" );
    // ... code ...

which will create an empty file and immediately rename it, without a need to close it at any point.

Upvotes: 5

sehe
sehe

Reputation: 393114

Boost Filesystem V3 doesn't provide a touch(1) function;

Even touch will creat+close a file, just look at the output of strace:

open("/tmp/q", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 47
dup2(47, 0)                             = 0
close(47)                               = 0
utimensat(0, NULL, NULL, 0)             = 0

I think your most reasonable bet is to just create a wrapper function that closes the file.

Upvotes: 10

Related Questions