检查文件在 c++ 中是否为只读
Check if a file is readonly in c++
我想检查 Windows 系统的 .ini 文件 c++ 中是否为只读
文件例如:
if(file == readonly){
//Do this
}
到目前为止,我已经尝试过这个并且它起作用了:
ofstream ofs(filename); //test to see if the file successfully opened for writing or not
但结果它会删除.ini文件并将文件留空
附注:1.it 不是重复的2.我在互联网上搜索了所有内容,发现了一些可以工作的示例,但结果没有按预期进行编译。
如果你在Windows上,你可以使用:
DWORD attr = GetFileAttributes(filename);
if (attr != INVALID_FILE_ATTRIBUTES) {
bool isReadOnly = attr & FILE_ATTRIBUTE_READONLY;
}
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 检查输入是否不是整数或数字
- 是否可以初始化不可复制类型的成员变量(或基类)
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 此代码是否违反一个定义规则
- 是否可以在C++中使变量真正只读
- 当我在C++中调用 struce 的只读静态成员时,线程是否安全
- 如何测试文件是否被锁定和/或只读而不打开?
- 检查文件在 c++ 中是否为只读
- 取消引用指向不同线程中的原子对象的只读非原子指针是否安全
- 在编译时确定这是否是只读的
- c++ 字符串类是否只读
- 是否可以以只读模式连接到内存中的sqlite数据库?
- 是否有任何运行时断言,如:内存是只读的还是当前进程可访问的
- 是否允许std::map在只读操作后重新平衡(如Splay树)
- 判断文件是否可以被创建/在c++中是否是只读的
- 为什么 QList::at() 不检查索引是否存在并返回只读值?
- 同时从多个线程访问只读数据是否明智?