Checking read-only status with C/C++

Programming, for all ages and all languages.
Post Reply
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Checking read-only status with C/C++

Post by Solar »

For a project of mine I have to check whether a file might be read-only. The language is C/C++... now, there's no such function in the standard lib that tests this for me; is there some elegant way except for trying to write to a file and see if that fails?
Every good solution is obvious once you've found it.
BI lazy

Re:Checking read-only status with C/C++

Post by BI lazy »

aren't system calls available which return some file information, like: Is the file locked, is the file in read only state (are the chmod bits for writing set?)?

For this, I think you'll have to issue a system call by yourself - outside the standard lib.

In Linux, on a quick glance, I've found fstat an fstatfs for this kind of info.

check this site for more info:
http://www.opengroup.org/onlinepubs/007 ... fstat.html
Tim

Re:Checking read-only status with C/C++

Post by Tim »

On Windows, GetFileAttributes gives you the state of the read-only bit. If you're worried about not having write permission to a file on an NTFS volume, the easiest way would be to attempt to write to the file, and handle ERROR_ACCESS_DENIED gracefully.

Anyway, this is all OS-dependent.
Post Reply