GT.M provides access to sequential files. These files allow linear access to records. Sequential files are used to create programs, store reports, and to communicate with facilities outside of GT.M.

The following table describes all READ and WRITE operations for STREAM, VARIABLE, and FIXED format sequential files having automatic record termination enabled (WRAP) or disabled (NOWRAP).

Command

WRAP or NOWRAP

STREAM or VARIABLE format file behavior

FIXED format file behavior

READ format or WRITE or WRITE *

WRAP

Write the entire argument, but anytime $X is about to exceed WIDTH: insert a <LF> character, set $X to 0, increment $Y

Similar to VARIABLE but no <LF>

READ format or WRITE or WRITE *

NOWRAP

Update $X based on STREAM or VARIABLE format as described below

Same as VARIABLE

STREAM: Write all of the argument with no truncation nor with a line terminator being inserted. Add length of argument to $X.

VARIABLE ($X=WIDTH): Write up to WIDTH-$X characters. Write no more output to the device until a WRITE ! or a SET $X makes $X less than WIDTH.

READ or WRITE !

either

Write <LF>, set $X to 0, increment $Y

Write PAD bytes to bring the current record to WIDTH

WRITE #

either

Write <FF>,<LF> or <FF>, set $X and $Y to 0. See the documentation for the USE and OPEN commands to understand how the [NO]FFLF device parameter and the gtm_nofflf environment variable affects the operation of "WRITE #". If the device parameter is unused, and the environment variable is unset, the default behavior of "WRITE #" is to write <FF>,<LF>.

Write PAD bytes to bring the current record to WIDTH, then a <FF> followed by WIDTH-1 PAD bytes

CLOSE

either

After a WRITE, if $X > 0, Write <LF>

After a WRITE, if $X >0, perform an implicit "WRITE !" adding PAD bytes to create a full record. If you need to avoid trailing PAD bytes set $X to 0 before closing a FIXED format file.

READ X

either

Return characters up to $X=WIDTH, or until encountering an <LF> or EOF. If <LF> encountered, set $X to 0, increment $Y

Return WIDTH characters; no maintenance of $X and $Y, except that EOF increments $Y

READ X#len

either

Return characters up to the first of $X=WIDTH or len characters, or encountering a <LF> or EOF; if up to len characters or EOF update $X, otherwise set $X to 0 and increment $Y

Return MIN(WIDTH, len) characters; no maintenance of $X and $Y, except that EOF increments $Y

READ *X

either

Return the code for one character and increment $X, if WIDTH=$X or <LF> encountered, set $X=0, increment $Y; if EOF return -1

Return the code for one character, if EOF return -1; no maintenance of $X and $Y, except that EOF increments $Y

[Note]Note
  • EOF == end-of-file; <FF>== ASCII form feed; <LF> == ASCII line feed;

  • In M mode, and by default in UTF-8 mode PAD == <SP> == ASCII space.

  • "READ format" in this table means READ ? or READ <strlit>

  • A change to WIDTH implicitly sets WRAP unless NOWRAP follows in the deviceparameter list

  • In VARIABLE and STREAM mode, READ (except for READ *) never returns <LF> characters

  • In M mode, the last setting of RECORDSIZE or WIDTH for the device determines WIDTH

  • In M Mode, a WRITE to a sequential device after setting $X to a value greater than the device WIDTH or a reducing WIDTH to less than the current $X acts as if the first character caused $X to exceed the WIDTH induces an immediate WRAP, if WRAP is enabled

  • In UTF-8 mode, RECORDSIZE is in bytes and WIDTH is in characters and the smaller acts as the WIDTH limit in the table.

  • In UTF-8 mode, FIXED mode writes <SP> to the RECORDSIZE when the next character won't fit.

  • In UTF-8 mode, all READ forms do not return trailing PAD characters.

  • In UTF-8 mode, all characters returned by all forms of FIXED mode READ are from a single record.

  • WRITE for a Sequential Disk (SD) device works at the current file position, whether attained with APPEND, REWIND or SEEK.

  • GT.M manages any BOM for UTF mode files by ensuring they are at the beginning of the file and produces a BOMMISMATCH error for an attempt to change the byte-ordering on OPEN for an existing file.

  • An attempt to OPEN a non-zero length file WRITEONLY without either NEWVERSION or TRUNCATE in UTF mode produces an OPENDEVFAIL due to the fact that any existing BOM information cannot be verified.

  • GT.M SD encryption, because of the state information associated with encryption processing, requires encrypted files to be WRITEn or READ from the beginning rather than from an arbitrary position.

  • HEREDOCs in shell scripts that drive input to GT.M present to GT.M as SD files, and by default terminate with a success - zero (0) status unless GT.M terminates with a ZHALT that supplies an alternative status.

The following tables provide a brief summary of deviceparameters for sequential files grouped into related areas. For more detailed information, refer to a??Opena??, a??Usea??, and a??Closea??.

File Format Deviceparameters

DEVICEPARAMETERS

COMMAND

COMMENT

[NO]FIXED

O

Controls whether records have fixed length.

[Z]LENGTH=intexpr

U

Controls virtual page length.

RECORDSIZE=intexpr

O

Specifies maximum record size.

STREAM

O

Specifies the STREAM format.

VARIABLE

O

Controls whether records have variable length.

[Z]WIDTH=intexpr

U

Controls maximum width of an output line.

[Z][NO]WRAP

O/U

Controls handling of records longer than device width.

O: Applies to the OPEN command

U: Applies to the USE command

C: Applies to the CLOSE command

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

Example:

GTM>do ^FREAD
FREAD;
 zprint ^FREAD 
 read "File > ",sd
 set retry=0
 set $ztrap="BADAGAIN"
 open sd:(readonly:exception="do BADOPEN")
 use sd:exception="goto EOF"
 for  use sd read x use $principal write x,!
EOF;
 if '$zeof zmessage +$zstatus
 close sd
 quit
BADOPEN;
 set retry=retry+1 
 if retry=2 open sd
 if retry=4 halt
 if $piece($zstatus,",",1)=2 do  
 . write !,"The file ",sd," does not exist. Retrying in about 2 seconds ..."
 . hang 2.1
 . quit 
 if $piece($zstatus,",",1)=13 do  
 . write !,"The file ",sd," is not accessible. Retrying in about 3 seconds ..."
 . hang 3.1
 . quit
 quit
BADAGAIN;
 w !,"BADAGAIN",!
 
File >

This example asks for the name of the file and displays its contents. It OPENs that file as READONLY and specifies an EXCEPTION. The exception handler for the OPEN deals with file-not-found and file-access errors and retries the OPEN command on error. 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:

GTM>do ^formatACCT
formatACCT;
 zprint ^formatACCT; 
 set sd="temp.dat",acct=""
 open sd:newversion 
 use sd:width=132
 for  set acct=$order(^ACCT(acct)) quit:acct=""  do  
 . set rec=$$FORMAT(acct)
 . write:$y>55 #,hdr write !,rec
 close sd
 quit

This OPENs a NEWVERSION of file temp.dat. 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 into more manageable blocks. The program writes a header record (set up in initialization and not shown in this code fragment) every 55 lines, because that is the application page length, allowing for top and bottom margins.

loading table of contents...