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?
Asked
Active
Viewed 1.9k times
10

Armen Tsirunyan
- 130,161
- 59
- 324
- 434
2 Answers
10
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.

sehe
- 374,641
- 47
- 450
- 633
4
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.

Christian Severin
- 1,793
- 19
- 38