views:

108

answers:

2

I'm wondering whether making files read-only so the user can't mess with them will disallow my program from writing information to them via an fstream.

A: 

If you open a file read-only, you can't write to it.

If you are looking to open a file that you can write to but nobody else can, then (in Windows) you are looking for file sharing attributes.

John Dibling
you mean ACLs - access control lists
pm100
ACLs would be rather pointless here, even if the program was running as a different user. In that case, the user would need credentials to run a program as that other user, and so, any file writable by the program becomes writable by the user as well.
Chinmay Kanchi
+1  A: 

Yes. If a file is read-only, it's read-only. Why not unset the read-only bit, write to the file, and reset it? The lock that you get on the file while writing to it should prevent users from making modifications to it while your application is writing to it. However, IMHO, the whole exercise is pointless, since it takes exactly 4 clicks to make a file writable, so your users can change the file whenever they want anyway. What I'd do is make an md5 or sha1 hash of the file, store it in the registry and check to see if that's changed on application startup.

Chinmay Kanchi
Of course, the user can still edit the registry...
Mike Daniels
Of course s/he can. But if you choose a good salt for the hash, the user will have some difficulty generating an acceptable hash for the file. Then of course, the user will have to get the salt from inspecting the program in a debugger or similar, and that's a whole different can of worms :).
Chinmay Kanchi