当您通过“属性”将文件设为只读时,您的程序可以直接写入该文件吗?
我想知道将文件设置为只读以便用户不能乱搞它们是否会禁止我的程序通过 fstream 向它们写入信息。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。如果文件是只读的,那么它就是只读的。为什么不取消设置只读位,写入文件,然后重置它?写入文件时获得的锁定应防止用户在应用程序写入文件时对其进行修改。然而,恕我直言,整个练习毫无意义,因为只需单击 4 次即可使文件可写,因此您的用户可以随时更改文件。我要做的就是创建文件的 md5 或 sha1 哈希值,将其存储在注册表中,并检查应用程序启动时是否发生更改。
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.
如果以只读方式打开文件,则无法对其进行写入。
如果您要打开一个您可以写入但其他人无法写入的文件,那么(在 Windows 中)您需要查找 文件共享属性。
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.