检查文件在 c++ 中是否为只读

Check if a file is readonly in c++

本文关键字:是否 只读 c++ 文件 检查      更新时间:2023-10-16

我想检查 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; 
}