简体   繁体   English

如何检查C ++代码中的文件写入权限?

[英]how can i check file write permissions in C++ code?

In my C++ program, I want to make sure i can write info to a file. 在我的C ++程序中,我要确保可以将信息写入文件。 How can I perform this check? 如何执行此检查?

The only sure way to find if you can write to a file is to try to write to a file. 确定是否可以写入文件的唯一确定方法是尝试写入文件。 It is possible, and even likely in some circumstances, that a file will have its permissions changed between calls to function such as stat and the actual write. 在调用某些函数(例如stat和实际写入)之间,可能甚至在某些情况下更改文件的权限。 Your code must deal with write failures anyway, so it makes sense to centralise the testing code there. 无论如何,您的代码都必须处理写入失败,因此在此处集中测试代码是有意义的。

您使用stat()系统调用,纯粹主义者会告诉您该调用不存在,除非您更改问题的标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM