--- In [email protected], "glogic20" <glo...@...> wrote: > ... Im writing code that takes data from a text file that > updates the positions of my 3d models on a frame per frame > basis(.bvh). I am currently writing code that reads in new > line each frame (each new line contains new frame data) but > i am currently opening the file each frame retrieving the > line i need and closing it again.
If you're reading the same file each time, it is grossly innefficient to constantly close and re-open. When you close a file, the operating system may release any cache associated with that file. So when you re-open it, it may have to reload that cache. [Note that drives themselves often perform look-ahead reads in anticipation.] Plus, what if the file changes in the mean time? How are you tracking where you're up to? Note that doing an ftell on one stream (especially text), then using that to fseek on another stream is not guaranteed to be reliable (or fast.) > As im learning from tutorials that all show closing the > file each time u have taken what you need within a game > loop. It's important to close files, even if you're only reading them. However, general practice is to read everything, then close it. Is there a reason why you can't load the whole file at once? [Or use mmap, if available.] > I know its a stupid question but i just want to know if > there is any danger in having a text file opened for the > length of the animation.i.e i open it when i load up my > .bvh file and then just read from it each frame and close > it once i read the end of file. The only danger is that you'll likely block any write access to that file (from say another program). But since you probably don't want the file to change over the coarse of the animation, that's a plus, not a minus. > Im writing it in C++ and i dont need any code just need > to understand/know if there is any dangers in just > leaving the file open in code that i should be aware of > in future coding. A much lesser danger is that you may run out of file descriptors if you're opening lots of other files at the same time. Leaving lots of open files can impact systems, but only in _extreme_ situations (e.g. opening and performing I/O on many hundreds of files simultaniously.) -- Peter
