CLOSING FILES IN KYAN PASCAL by Erik Warren, 18 March 1987 _______________________________________ To us Kyan Tech Support guys, it seems as though everybody and their mother asks us a derivative of this question: "In Kyan Pascal, how do I close a file after I am done with it?" The answer: You don't. The manner in which Kyan Pascal handles the closing of files is known as 'Good Housekeeping' or 'Lazy I/O.' Instead of requiring the programmer to issue a Close command, the compiler closes the file when the program exits the procedure or function in which the file was declared and opened. So, if your program uses global files, they will not be closed until the final "END." of the program is encountered. If you are using a lot of files, you may eventually encounter a DOS error like 'too many open files.' (That'll teach ya! You shouldn't be using global files/variables, etc. anyway; you should think of modular coding with local files/variables.) With the following program... PROGRAM abc(Input,Output,Fyle); VAR Fyle : Text; BEGIN Reset(Fyle,'D:FILENAME.EXT'); . . . END. ...Fyle will NOT be closed until END. is reached. Here is how you should make Fyle local... PROGRAM abc(Input,Output); PROCEDURE xyz; VAR Fyle : Text; BEGIN Reset(Fyle,'D:FILENAME.EXT'); . . . END; (* of procedure xyz *) BEGIN xyz; . . . END. (* of main program *) When xyz is called in the above program, Fyle will be opened and then closed when xyz is exited. _______________________________________ THE FURTHER ADVENTURES OF JOE PROGRAMMER_______________________________________ Now let's suppose my good friend Joe Programmer programs himself into a hopeless situation where he doesn't want to exit a procedure to close a file. In addition to being the kind of person who paints himself into the corner of a room, Joe also programs himself into corners. (For this reason and others, Joe Programmer is the world's most frequent user of Pascal's Label and Goto statements.) Lucky for him, just as a fighter pilot can eject from a burning plane, Joe also has a last-ditch 'Bail Button.' This unorthodox, back-door approach of closing files is done via CIO... FUNCTION Close(IOCB_Num : Integer) : Integer; BEGIN IOCB_Num := IOCB_Num * 16; Close := 0; #A STX _t ;for safety's sake LDY #7 ;sp offset to IOCB_Num LDA (_sp),y ;pull user's IOCB # TAX ;accum. to X reg. LDA #12 ;close command STA $342,x ;store in ICCMD (ICCOM) LDA #$00 ;zero out aux. bytes... STA $034A,x ;one... STA $034B,x ;and two JSR $E456 ;jump via CIO vector TYA ;Y reg. to accum. LDY #5 ;sp offset to Close LSB STA (_sp),y ;store CIO's error code LDX _t ;safety # END; This function is called like this: Status := Close(1); The integer you pass to it is the IOCB number you wish to Close. Since you don't usually know which IOCB# the compiler has allocated for your file(s) you may want to make a loop to close multiple IOCB's. Make sure not to close #0 or #6. The integer returned is the CIO status byte; a '1' would indicate everything is okay. Remember, this is the "Wimp's Way Out." The preferable way to close a file is to exit the block of code it was opened in.