Checking read-only status with C/C++
Checking read-only status with C/C++
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.
Re:Checking read-only status with C/C++
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
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
Re:Checking read-only status with C/C++
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.
Anyway, this is all OS-dependent.