I am preparing a lecture on files for the more junior students in programming class. One of the points I want to elaborate are good practices with files.
What are the thing to keep in mind when using files in any programming language?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’ve found that junior programmers often have poor intuition or learned incorrect lessons about the speed of accessing files.
Very new programmers assume that files are very fast and need help understanding why reading one byte at a time from an unbuffered file is a bad idea. Similarly, accessing directory information can be very slow and should be cached if possible.
Unfortunately, some more experienced programmers learn the wrong lesson and assume that everything always needs to be cached in RAM or it will be too slow. Modern operating systems have very sophisticated disk caches, so the second time you access the same part of a file might be significantly faster.
Finally, interactive programs should do all file operations in another thread, so your application doesn’t slow to a crawl or stop working when the disk is busy, or when a remote volume is temporarily unavailable.