the_Begin
the_Begin

Reputation: 461

Check if a file is readonly in c++

I want to check if a .ini file is readonly or not in c++ for windows systems

for example:

if(file == readonly){
  //Do this
}

so far i have tried with this and it worked:

ofstream ofs(filename); //test to see if the file successfully opened for writing or not

but as a result it would delete the .ini file and it leave the file blank

PS: 1.it is not a duplicate 2. I have searched all over internet and i found some examples that could have worked but as a result the did not compiled as they should.

Upvotes: 1

Views: 3580

Answers (1)

StPiere
StPiere

Reputation: 4263

If you're on Windows you can use:

DWORD attr = GetFileAttributes(filename);
if (attr != INVALID_FILE_ATTRIBUTES) { 
    bool isReadOnly = attr & FILE_ATTRIBUTE_READONLY; 
}

Upvotes: 9

Related Questions