Author Topic: File already open errors  (Read 4359 times)

0 Members and 1 Guest are viewing this topic.

Offline lawsonm1

  • Newbie
  • Posts: 64
    • View Profile
File already open errors
« on: June 10, 2020, 04:39:37 pm »
IN the process of putting together this program to get stock data and display it, I use wGet to retrieve the data into a file, via a SHELL execution of a batch file, then I read the data from each file into the program. Every once and awhile I get errors of each of the file. Error 55 followed by error 62.

I thought it might be that the wGet/batch created files hadn't been closed, so I added a 30 second delay after the line that executes the SHELL command (SHELL _HIDE "CMD /C MoneywGet.bat"). But that hasn't seemed to work.

I also added an error handler routine (ON ERROR GOTO Errhandler)...

Code: QB64: [Select]
  1. Errhandler: 'error handler sub program line label
  2. LOCATE 126, 80: PRINT "Error"; ERR; "on program file line"; _ERRORLINE
  3. BEEP ' warning beep
  4. OPEN "LiveBigBoardErrorLog.txt" FOR APPEND AS #2
  5. 'RESUME NEXT ' moves program to code following the error.
  6. GOTO loop1

But that still posts the error popups, and it never really goes back to the beginning (Loop1), it just goes and displays the next error popup. Any thoughts on what I am doing wrong; or missing? Thanks, Mike

Offline lawsonm1

  • Newbie
  • Posts: 64
    • View Profile
Re: File already open errors
« Reply #1 on: June 10, 2020, 04:41:45 pm »
Oh, I forgot this question, if the first error is a "File already open" error, how can I test to see if the file shows open and then just post an error message in the program and loop until the file closes? Thanks, Mike

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: File already open errors
« Reply #2 on: June 12, 2020, 05:52:51 pm »
Hi.

Without bug solutions, just for printing error codes:

Code: QB64: [Select]
  1. ON ERROR GOTO errorhandler
  2. OPEN "FileExist.avi" FOR OUTPUT AS FF
  3. OPEN "FileNotExist.avi" FOR INPUT AS FF
  4. ff2 = FREEFILE
  5. OPEN "FileNotExist.avi" FOR INPUT AS ff2
  6. CIRCLE (100, 100), 50, 66 'TRY DRAWING IN TEXT MODE
  7. LOCATE 126, 80 ' illegal function call in this text window. Max is 24, 80  or must be first used SCREEN _NEWIMAGE (126, 80, 0) - text screen, 126 rows, 80 columns.
  8.  
  9.  
  10. errorhandler:
  11. PRINT "Error: "; ERR
  12.     CASE 53: PRINT "File not found" 'create file and then return back
  13.     CASE 55: PRINT "File already opened (also, if you try two or more files opening under one number)" 'use FREEFILE for free numbers for opening next file [a = FREEFILE: OPEN "test.txt" FOR INPUT AS a...]
  14.     CASE 5: PRINT "Illegal function call" 'set graphic screen
  15.  
  16.  
  17.  
  18.  
  19. RESUME NEXT 'or use RESUME ----> program return to row in which error occur. First action for this bug reparing must be done in errorhandler.
  20.  
  21.  


and how run error handle and repairing some bug:

Code: QB64: [Select]
  1. ON ERROR GOTO errorhandler
  2. CIRCLE (100, 100), 50, 66 'TRY DRAWING IN TEXT MODE
  3.  
  4.  
  5. errorhandler:
  6. PRINT "Error: "; ERR
  7.     CASE 5: PRINT "Illegal function call"
  8.         BEEP
  9.         SLEEP 3
  10.         SCREEN 7 'set graphic screen
  11.  
  12.  
  13.  


Offline lawsonm1

  • Newbie
  • Posts: 64
    • View Profile
Re: File already open errors
« Reply #3 on: June 13, 2020, 09:44:59 pm »
Thanks Petr. I think part of my problem is that I used "GOTO Loop1" and not "RESUME Loop1". I was reading through the QuickBASIC Bible when I realized I should have used RESUME. I don't think the "file already open error" is coming from my program, since that file is only opened once in the program, and when the error occurs, it seems to be at different places in the program. The program reads about 2 dozen different files, but one at a time, and in a sequence; ie. open, read the data, close the file, manipulate the data, and then move on to the next one.

The problem occurs randomly and intermittently, and at different places in the program. Since the files begin to be read fairly quickly after the files are created (from a SHELL batch file), I am wondering if the files are flagged as opened due to the anti-virus program scanning them and they are not closed before my program wants to open and read them. I have excluded the entire folder from scanning by the AV program to see if that helps. But again, since it happens randomly and intermittently, it may be awhile before I am certain. Thanks, Mike

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: File already open errors
« Reply #4 on: June 14, 2020, 07:49:28 am »
Do you happen to use the CLEAR statement somewhere in the program?

First source works correctly in this form:

Code: [Select]
loop1:
Errhandler: 'error handler sub program line label

IF ERR THEN
    PRINT "Error"; ERR; "on program file line"; _ERRORLINE
    BEEP ' warning beep
END IF

OPEN "LiveBigBoardErrorLog.txt" FOR APPEND AS #2
PRINT #2, DATE$, TIME$, ERR, _ERRORLINE
CLOSE #2
'RESUME NEXT ' moves program to code following the error.
GOTO loop1

If you can, send me here your source code and i try look in.
« Last Edit: June 14, 2020, 07:57:24 am by Petr »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: File already open errors
« Reply #5 on: June 14, 2020, 12:32:16 pm »
Another way to handle these type of problems is to track the files yourself, with something similar to the following:

Code: [Select]
DIM SHARED FileHandles(100) 'for up to 100 files to be open at a time.

FH = GetHandle
OPEN "temp.txt" FOR OUTPUT AS FH
IF CheckHandle(FH) THEN
    PRINT FH; " is open"
ELSE
    PRINT FH; " is freed"
END IF
DO
    _LIMIT 10
LOOP UNTIL CloseHandle(FH) = 0 'A check to pause execution until the file is actually freed
IF CheckHandle(FH) THEN
    PRINT FH; " is open"
ELSE
    PRINT FH; " is freed"
END IF



FUNCTION GetHandle
    FOR i = 0 TO 100
        IF FileHandles(i) = 0 THEN
            FileHandles(i) = FREEFILE
            GetHandle = FileHandles(i)
            EXIT FUNCTION
        END IF
    NEXT
    GetHandle = 0 '0 represents a failed file handle attempt
END FUNCTION

FUNCTION CloseHandle (Handle)
    FOR i = 0 TO 100
        IF FileHandles(i) = Handle THEN CLOSE Handle: FileHandles(i) = 0: CloseHandle = -1 'success
    NEXT
    'a return value of 0 means the handle wasn't found, nor freed.
END FUNCTION

FUNCTION CheckHandle (Handle)
    FOR i = 0 TO 100
        IF FileHandles(i) = Handle THEN CheckHandle = -1 'handle found
    NEXT
    'a return value of 0 means the handle wasn't found.
END FUNCTION
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline lawsonm1

  • Newbie
  • Posts: 64
    • View Profile
Re: File already open errors
« Reply #6 on: June 15, 2020, 05:08:07 pm »
So far so good, without errors, since I excluded the files from A/V scanning. I'm in the process of fixing some math errors, and then I want to move some of the program constants to a disk file that can be read at the start of the program. Then I plan on posting to code for comment. Thanks again, Mike

Offline lawsonm1

  • Newbie
  • Posts: 64
    • View Profile
Re: File already open errors
« Reply #7 on: June 21, 2020, 09:53:27 pm »
Well, I had it happen again today. The program was running fine and all of the sudden it started to show up. Hold on - stop the presses. I think I found my error. So I open a file for input, read the data. If there is some data missing, then I would have reached an EOF. Since the data is missing, I stop further processing and go to the next file to process. But it seems I neglected to Close the previously opened file! I am so stupid sometimes! Well I just added some preemptive safeguarding code to the start of each block of file loading and processing to Close the file number just in case. Mike