When I was chatting with Swivel a long time ago about writing to files, he said to stay away from fopen/fread/fclose (forgot the reason) and to use some different functions instead. I'll edit my post in a sec with the different functions. But, why should I stay away from fopen/close/read? And what is the right way to write to a file?
It depends what you're using the files for. One very important function is called flock(); if you have multiple pages that are writing the same file, possibly simultaneously, one needs to lock the file, write its contents, then release the lock so other page(s) (or clients, or instances, etc) can acquire the lock and work their magic. I think the larger question is whether a file is the right thing to use for what you want to do.
If I wanted to make a chat program? (Or moreso, that game we were playing in IRC earlier Razz) Is there a "file_is_locked()" or similar? It will mainly be a .txt file btw Smile
_player1537 wrote:
If I wanted to make a chat program? (Or moreso, that game we were playing in IRC earlier Razz) Is there a "file_is_locked()" or similar? It will mainly be a .txt file btw Smile


Take a look at this link for details:

http://us.php.net/flock

You use LOCK_EX to acquire an exclusive lock, and LOCK_UN to release that lock. I can't see a case where you would need a LOCK_SH (shared lock) for this application. You can use LOCK_NB (provided that your server is running non-Windows) to test whether a lock is acquirable or held.
Why are you opening files? Use databases instead.
allynfolksjr wrote:
Why are you opening files? Use databases instead.
He'll get to databases, but I see no harm in him learning a little file manipulation first.
Late, but is there anything bad about sticking a lot of data in a database? Is there any time I should use a file instead of SQL, in other words?
More or less, I think it's a hosting thing. A lot of hosts will generally frown upon if not outright rule against anything that intensively accesses a database.

Beyond that, I don't think there's anything bad about intensive database usage.
Umm, for large amounts of data I think SQL is better, but for small amounts of data SQL would be slower than just reading from a file.
Deep Thought wrote:
Late, but is there anything bad about sticking a lot of data in a database? Is there any time I should use a file instead of SQL, in other words?
It mostly depends on what you're trying to do with that data. If you want to randomly access and search it, then a file is a bad idea. However, if it's big chunks of data that you're going to be iterating over, a few flatfiles are better.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement