Sequential File Examples

This section contains a few brief examples of GT.M sequential file handling.

Example:

    READ "File > ",sd
    OPEN sd:(readonly:exception="G BADOPEN")
    USE sd:exception="G EOF"
    FOR USE sd READ x USE $PRINCIPAL WRITE x,!
    EOfIF '$ZEOF ZM +$ZS
    CLOSE sd
    QUIT
    BADOPEN IF $ZS["-FNF," DO QUIT
    . WRITE !,"The file ",sd," does not exist."
    IF $ZS["-PRV," DO QUIT
    . WRITE !,"The file ",sd," is not accessible."
    ZM +$ZS
    QUIT
    

This example OPENs a file READONLY and specifies an EXCEPTION. The exception handler for the OPEN deals with file-not-found and file-access errors, and reissues all other errors with the ZMESSAGE command. The first USE sets the EXCEPTION to handle end-of-file. The FOR loop reads the file one record at a time and transfers each record to the principal device. The GOTO in the EXCEPTION terminates the FOR loop. At label EOF, if $ZEOF is false, the code reissues the error that triggered the exception. Otherwise, the CLOSE releases the file.

Example:

    SET sd="temp.dat",acct=""
    OPEN sd:newversion U sd:width=132
    FOR SET acct=$O(^ACCT(acct)) QUIT:acct="" DO
    . SET rec=$$FORMAT(acct)
    . WRITE:$Y>55 #,hdr W !,rec
    CLOSE sd:(spool:delete)    
    

This OPENs a NEWVERSION of file TEMP.DAT. The USE sets the line width to 132 characters. The FOR loop cycles through the ^ACCT global formatting (not shown in this code fragment) lines and writing them to the file. The FOR loop uses the argumentless DO construct to break a long line of code for greater readability. The program writes a header record (set up in initialization not, shown in this code fragment) every 55 lines, because that is the application page length, allowing for top and bottom margins. Finally the CLOSE releases the file to the VMS spooler for printing on the default print queue SYS$PRINT and also for deletion when the printing finishes.