WRITESEQ Statement

Appends data to a host file or Reality item that has been opened for sequential access.

Syntax

WRITESEQ data {:} [ON || TO] seqfile-var {SETTING setting-var} {ON ERROR statement(s)} [ THEN statement(s) | ELSE statement(s) ]

Syntax Elements

dataA string expression that evaluates to the data to be written. An appropriate line delimiter is appended to the data, as follows:

UNIX file: Line feed - CHAR(10).

Windows file: Carriage return/line feed - CHAR(13):CHAR(10).

Reality item: Attribute mark.

: Specifies that no line delimiter should be appended to the data written. WRITESEQ then behaves like the WRITEBLK statement.

seqfile-varis the name of a variable containing a sequential file reference (assigned via an OPENSEQ statement).

setting-varThe name of a variable to which the return status is assigned. See Operation for details of the possible values for this variable.

statement(s)A sequence of one or more valid DataBasic statements, either separated by semicolons, or contained on separate lines and followed by an END statement.

Operation

The following details circumstances under which the different clauses are executed and the values returned in setting-var:

Clause Executed

Reason

setting-var

STATUS() Value

THEN

Write successful

Current position

0

ELSE

Not at end of file or item

2420

206

ON ERROR

File or item opened, but not for sequential access (not opened with OPENSEQ)

B45

12

 

File or item not open

B12

12

 

General file error

Error number

Error number

Note

  • If an error occurs but there is no ON ERROR clause, the DataBasic Debugger will be entered with error B53 as well as the setting-var value.

  • The final column give the values returned by the STATUS function if this is used instead of a SETTING clause.

Comments

You can only write if the current position is at the end of the file (see Writing to a Sequential File).

If the file or item does not exist, it is automatically created.

If a record size was specified when the file was opened for sequential access, the data will be padded with NUL characters (X'00') until its length is a multiple of the record size. The appropriate line delimiter is then appended to the padded data.

It is recommended that you always include an ON ERROR clause. If you do not and an error occurs at run time, your program will abort. This is particularly important if the file has a PRE-WRITE trigger that may cancel the WRITESEQ operation.

Example

OPENSEQ "/usr/pauls/qa4" TO SF1 THEN WEOFSEQ SF1
NUMFIELDS = DCOUNT(DATA, @AM)
LOOP VARYING I = 1 WHILE I <= NUMFIELDS DO
    WRITESEQ DATA<I> TO SF1 THEN NULL
REPEAT
CLOSESEQ SF1

This program opens the UNIX file /usr/pauls/qa4 and saves a reference to the open file in variable SF1. If the file already exists, it is truncated at the start so that the existing contents will be overwritten. The program then loops through the attributes in the dynamic array DATA, writing each attribute to the file as a line delimited with a line feed. Finally, it closes the file.

See Also

WRITEBLK statement, OPENSEQ statement, CLOSESEQ statement, READSEQ statement, READBLK statement, SEEK statement, DELETESEQ statement, WEOFSEQ statement.