Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / General 2 / September 2004



Tip: Looking for answers? Try searching our database.

Read/Write Open/Close

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
HMdesigner - 28 Sep 2004 19:55 GMT
Hello.

I'm reading and writing to a random .dat file I've created, and I'm doing it
often. My instinct is to open and close the file each time, even though that
may occur seconds apart ... I like it nice and neat.

Anyone have strong feelings against this? For frequent I/O operations do I want
to keep files open and just get/put them?

Thanks for your time.
HMdesigner
David Pendrey - 29 Sep 2004 00:24 GMT
There are several things to consider with this. If it is some kind of log
file, it should remain open to minimise the system resources being used by
the log file (although it uses a file handle continuously, it would be in
use fairly often anyway and opening and closing takes time + memory).
However if it is a shared/communication file between two concurrent programs
you should open and close it each time to keep it clean (I'd make a
WriteDataToFile function to make it easier to).

It also depends on how many other files you are using in your program and
how you get their file numbers. You can statically assign each file a
specific number, or use the FreeFile function, each method having its own
advantages and disadvantages. Basically static is good for when there are a
fixed number of files used by your program (log file, INI file, user data
file, backup data file), whereas using FreeFile is better if you are making
a program where there are going to be a random number of files open (like a
word processing program). If you are making a program with a random number
of open files I would suggest not using static numbering even for the
constantly open files (log files, etc) as if an error occurs and you need to
close then open the file, in the mean time the user could have opened
another file and used that file number.

Bottom line is do it whatever way you would find easier to code. If you
really want the optimum code for your program then the best thing would be
to try it both ways and measure the CPU and memory usage while the program
is running. If your running windows XP this is REALLY easy. Just open the
task manager and go into the Processes tab. Open up Visual Basic from
scratch and start running your program. Then take a note of the CPU time and
Peak Mem Usage fields in the table there for visual basic (or your program
depending on the version of visual basic).

Hope this helps.
J French - 29 Sep 2004 07:45 GMT
>Hello.
>
[quoted text clipped - 4 lines]
>Anyone have strong feelings against this? For frequent I/O operations do I want
>to keep files open and just get/put them?

Opening files is time consuming
- there is a very substantial 'hit' on opening files.

However if a file has been updated and will not be re-used in the next
few seconds, I always close the file and re-open it to ensure that the
data really hits the disk.

One system crash with a partially updated open file wipes out any
speed gains.

I also pre-extend files if possible before writing the data, then
close them and re-open them to ensure that the Directory info matches
that in the FAT
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.