如果文件不存在,有没有办法创建和打开文件,否则会失败? [英] Is there any way to create and open a file if it doesn't exist but fail otherwise?

查看:38
本文介绍了如果文件不存在,有没有办法创建和打开文件,否则会失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它看起来像 OpenOptions 不支持这种情况,现有文件将被截断或覆盖.

It looks like OpenOptions does not support this scenario and an existing file will either be truncated or overwritten.

推荐答案

从 Rust 1.9.0 开始,有 OpenOptions::create_new 它使您能够安全且原子地确保您正在创建新文件,并且您的命令将失败除此以外.

As of Rust 1.9.0, there is OpenOptions::create_new which enables you to safely and atomically ensure that you are creating a new file, and that your command will fail otherwise.

这篇关于如果文件不存在,有没有办法创建和打开文件,否则会失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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