I am making a simple page load counter by storing the current count in a file. This is how I want to do this:
Yes, you have to use rewind before ftruncate. Otherwise, the old content of the file is only refilled with zeros.
ftruncate
The working sequence is:
fopen flock LOCK_EX fread filesize rewind ftruncate 0 fwrite flock LOCK_UN fclose