Reputation: 698
In windows OS, in my C++ application, I have to protect the entire file which is already existing and I should be able to do read and write operation against that file.
Before closing the file I should protect the file so that other than my application or any user cannot access that file?
How to achieve this in C++?
I am using
FILE * fp;
fp = fopen ("file.txt", "w+");
Thanks in advance.
Upvotes: 0
Views: 4003
Reputation: 28932
I do not believe that this is possible using standard C or C++ or with a FILE*
object directly. Having said that Windows has the LockFile
and UnlockFile
APIs and you can achieve the same thing on Posix systems with fcntl
Upvotes: 0
Reputation: 11178
C++ does not provide file locking or file security functions so you have to rely on Platform API. In Windows, it's LockFileEx() and UnlockFileEx() while the file is opened, and File Security and Access Rights when the file is closed.
That said, security in Windows is a highly complex subject so you should describe what's the goal you are trying to achieve. I think you need to disallow other apps to read your file, so in that case, you have to encrypt the file with a password.
Upvotes: 2