Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 334
Armen Tsirunyan Avatar asked May 04 '15 11:05

Armen Tsirunyan


People also ask

How do you create a CPP file?

To create a file, use either the ofstream or fstream class, and specify the name of the file. To write to the file, use the insertion operator ( << ).

Is boost filesystem header only?

Unlike a large portion of the Boost ecosystem, boost::filesystem is not header-only. However, integration into a project was quite simple, and the functionality it provided was impressive.

What is Boost filesystem?

The Boost Filesystem Library provides portable facilities to query and manipulate paths, files, and directories. The motivation for the library is the need to be able to perform portable script-like operations from within C++ programs.


1 Answers

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.

like image 123
sehe Avatar answered Sep 22 '22 11:09

sehe