How to create a file with boost file system without opening it

The forced file system has a create_directory function that creates a directory. How to create a file? I could create it by specifying the boost::filesystem::ofstream , but which would also open the file, so I would have to call close on it before I could do other things with it, such as renaming or deleting. Is this the only way?

+6
source share
2 answers

Boost V3 file system does not provide touch(1) ;

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

 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 simply create a wrapper function that closes the file.

+5
source

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 having to close it at any time.

+1
source

Source: https://habr.com/ru/post/986494/


All Articles