Page 1 of 1
Checking read-only status with C/C++
Posted: Fri Feb 06, 2004 3:51 am
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?
Re:Checking read-only status with C/C++
Posted: Fri Feb 06, 2004 4:47 am
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
Re:Checking read-only status with C/C++
Posted: Fri Feb 06, 2004 4:52 pm
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.