如何在不打开的情况下使用boost文件系统创建文件 [英] How do I create a file with boost filesystem without opening it

查看:72
本文介绍了如何在不打开的情况下使用boost文件系统创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在boost文件系统中,有一个函数 create_directory 创建目录.如何创建文件?我可以通过定义一个 boost :: filesystem :: ofstream 对象来创建一个对象,但这也会打开文件,因此我必须先对其调用 close 其他内容,例如重命名或删除.这是唯一的方法吗?

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?

推荐答案

Boost Filesystem V3不提供 touch(1)函数;

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

即使 touch 也会创建+关闭文件,只需查看 strace 的输出即可:

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.

这篇关于如何在不打开的情况下使用boost文件系统创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆