Open files

Blitz3D Forums/Blitz3D Beginners Area/Open files

Farflame(Posted 2005) [#1]
My program is constantly opening and closing files, and several times now I've run into serious bugs because I'd forgotten to close a file, which was then opened over and over and eventually caused a crash (too many open files).

To help me spot these bugs before they happen, I'm thinking of putting a something on the screen to tell me how many open files I have, so that I can spot problems before they start, but is there any way of doing it? Is there some way to check how many files are currently open?


jfk EO-11110(Posted 2005) [#2]
Maybe you should store all filenames, their handles and a flag that signals if they're open for writing or readin in some arrays or types, so when you open a file, simply parse the array ant see if it's already open. If you close a File, simply remove it from the arrays. You can do this by replacing the OpenFile and CloseFile Commands by a Functioncall that will do both, store infos in arrays plus open or close the files. So all you have to do is writing two functions, then use the Replace Tool to replace eg. every "CloseFile(" by "MyCloseFile(".


Rook Zimbabwe(Posted 2005) [#3]
You can also increment a count by one... ex:
foo=loadimage("idiot.png")
loadfiles = loadfiles+1
foo2=loadimage("idiot2.png")
loadfiles=loadfiles+1
etc...
I mean that seems to be what you are asking...
I think there is a way to check for fileopen using one of the DLLs in the toolbox. Look undeer File Utilitys and MISC.

Maybe...

Rook


Farflame(Posted 2005) [#4]
Hmmm, can't find that dll.

Strange little problem has occured, which I'm pretty sure is due to too many open files (does Blitz give a specific error about too many open files or does it just act strangely?). If I leave my program running for about 20-30 minutes, it eventually crashes, which I suspect is because some file is being constantly opened but never closed. The odd thing is, when I look at the debug list, the variable to open the file isn't on the list at all.

In other words, I have the line.....

PlayerFile=ReadFile("gamedata\players\playerdata.txt")

But when I look at the debug window, 'Playerfile' isn't even listed. Surely it should just be listed, but show a zero (meaning it failed to open). Right?


jfk EO-11110(Posted 2005) [#5]
Yes. How many files is "many"? Probably it's a windows problem. Also, you should not open a file twice without to close it. Of course there must be some stack that stores what files are open, and if you open the same file again and again, there may be some kind of stack overflow the sooner or the later.

Again, if you open a file twice, what for do you open it? Reading or wrinting? And if you open it for writing, do you want to overwrite it, or are you working with seek etc?


Perturbatio(Posted 2005) [#6]
whenever I use a command that requires a closing command (i.e. OpenFile), I immediately below it put the closing command and then add the code I need to inbetween. This way, I never have these issues.


Farflame(Posted 2005) [#7]
Yes, I'm starting to learn that the hard way. Luckily I found the last problem just by luck - I'll be more careful from now on when opening files.


Hotcakes(Posted 2005) [#8]
does Blitz give a specific error about too many open files or does it just act strangely

It acts strangely. It might even be a Windows problem maybe. I learnt very quickly that's a really good idea not to open or close files in a main loop ;] ie it's not a good idea to constantly open files even if you are not leaving any open! Not only is it a slow operation, but Windows does -not- like it...


jfk EO-11110(Posted 2005) [#9]
Windows also uses optimized File access, such as delayed writing or cached reading. I'm afraid this won't work 100% accurately in heavy stress situations.


Farflame(Posted 2005) [#10]
That's a bit of a worry because my server program is very heavy on disk usage. Is there some way to catch Windows erros without the program stopping?