Overwriting specific lines in text files without rewriting the whole thing.
| From: | John Biesnecker | Date: | Mon, 26 Jun 2000 20:22:22 +0000 |
| Subject: | Overwriting specific lines in text files without rewriting the whole thing. | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-3194@lists.php.net to get a copy of this message | ||
I have created rather simple file-based page hit counter for my site (because I can't use SQL
or anything else). Currently I am reading the entire thing into an array using file(), finding the
correct line, editing it and writing the entire thing back into a file.
After a few weeks of content development, however, my counter data file has gotten very large, and
reading and writing the entire file is beginning to take a noticable amount of time. I'm
already sorting the file so the most accessed pages are on top, hoping to reduce time needed to find
the correct entry.
I'm wondering if there is a way that while reading the file line by line and I can alter a
single line and write it back to the file without disturbing the rest of the lines and then exit.
Also, if there is, will I see any performance improvement worth the code rewrite.
Thanks.