FileClose not working?

Started by markpierzchala, November 27, 2021, 01:07:54 PM

Previous topic - Next topic

markpierzchala

I have a FileOpen, FileWrite, FileClose within a WHILE loop. The FileClose does not seem to work. The code follows. DD is a string variable that I compute above. As I watch the program execute, I can see in every WHILE loop that the value of zz decrements by 1: 127, 126, 125, etc. Sometimes I want to run the loop well over 128 times, but it stops after it exhausts the number of file handles. Then I get WinBatch error 3078.

   handle3 = FileOpen("History\KeyLists.csv", "APPEND")
   FileWrite(handle3, DD)
   FileClose(handle3)
   zz = IntControl(77, 21, 0, 0, 0)
   Display(2, "Number of file slots open: ", %zz%)

JTaylor

Why are you opening and closing within the While Loop?   If the file open count is decreasing then you shouldn't run out of handles anyway but still doesn't make sense, from what you have shared, why you are doing it this way.

Jim

markpierzchala

Thank you for your quick reply. I moved the opening and closing of the file outside the WHILE loop and that seems to have solved the problem.

As to why I tried it that way, who knows. I was trying many different things to make a number of things work. I don't think I started out that way. I knew it was inefficient, but it should have worked anyway. Where I come from that's a bug.

I use WinBatch every 2 or 3 years. I like WinBatch. I find the language strong, and the help mostly useful. But to determine how WinBatch works with the Windows operating system is a learning curve.

Nevertheless, that problem seems to be resolved. Thank you.

td

A bug? Following produces the expected result.
Code (winbatch) Select
Before = IntControl(77,21,0,0,0)
for x = 1 to 256
   hFile = FileOpen('C:\temp\dummy.txt','read')
   junk = FileRead(hFile)
   FileClose(hFile)
next
After = IntControl(77,21,0,0,0)

Message('File Handle Table Entries Available', 'Before: ':Before:@lf:'After: ':After)
exit


"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Works fine with FileWrite() as well:
Code (WINBATCH) Select


Before = IntControl(77,21,0,0,0)
for x = 1 to 256
   hFile = FileOpen('C:\temp\dummy.txt','append')
   junk = FileWrite(hFile,x)
   FileClose(hFile)
next
After = IntControl(77,21,0,0,0)


Message('File Handle Table Entries Available', 'Before: ':Before:@lf:'After: ':After)
exit

td

The FileOpen mode matters when closing a filehandle. The FileClose function does not consider or use the mode information in any way.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Quote from: td on November 29, 2021, 08:05:21 AM
The FileOpen mode matters when closing a filehandle. The FileClose function does not consider or use the mode information in any way.


Understood. Just tried to support with same modes used by OP.

td

That was a typo on my part (nothing new). The statement should have read "the FileOpen mode does not matter when closing a filehandle." Marty would have been proud...
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

kdmoyers

Whew. I had read those two sentences twenty times and finally concluded that there are things mortal man was never meant to understand.
-K
The mind is everything; What you think, you become.

td

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

seckner

Speaking of Marty, can I decompile this script?