I was curious how to lock a text file and unlock it?
I was curious how to lock a text file and unlock it?
You can't do that in standard C. Such a task requires something platform specific.
Linux Slackware.
What do you mean 'lock' ?
Make it so only you and the admins can read it?
Just do this at your command prompt,
chmod 400 myfile.txt
and it will be readable by you only.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Multiple ways but look at these before attempting:
1. http://linux.die.net/man/2/open
2. http://linux.die.net/man/2/flock
3. http://linux.die.net/man/2/fcntl (more complicated)
Some simplified steps are:
1. open() the file so you have a file descriptor to pass to flock.
2. now apply the lock using flock()
3. unlock with flock()
4. close() the file descriptor.
Also, keep in mind that locks pertain only to other programs following the locking mechanism.