WRITECSV

WRITECSV

Top  Previous  Next

 

The WRITECSV statement writes data at the current file position in a record previously opened using OPENSEQ. The data to be written is assembled from one or more variables and written in CSV format.

 

 

Format

 

WRITECSV var1, var2, ... {ENCODING name} TO file.var

{ON ERROR statement(s)}

{THEN statement(s)}

{ELSE statement(s)}

 

where

 

var1, var2, ...are the items to be written to the file. If any of the variables contains field marks, each field is treated as a separate item in the resultant CSV data.

 

file.varis the file variable associated with the file.

 

statement(s)are statements to be executed depending on the outcome of the WRITECSV operation.

 

At least one of the THEN and ELSE clauses must be present.

 

 

The optional ENCODING element to this statement sets the character encoding to be used, overriding any encoding set in the VOC F-type record or when the file was opened.

 

The data in the named variables is assembled as a CSV format text string which is then written to the file with a newline appended. The THEN clause is executed if the WRITECSV is successful.

 

If the record was opened with the SHARED option of OPENSEQ, the data will be appended to any existing record content.

 

The ELSE clause is executed if the WRITECSV fails. The STATUS() function will indicate the cause of the error.

 

The ON ERROR clause is executed for serious fault conditions such as errors in a file's internal control structures. The STATUS() function will return an error number. If no ON ERROR clause is present, an abort would occur.

 

 

CSV Format

 

CSV format is used by many applications. QM adheres to the CSV standard (RFC 4180).

 

Items are enclosed in double quotes if they contain commas or double quotes. Embedded double quotes are replaced by a pair of double quotes.

 

The CSV.MODE statement can be used to select variations on the quoting rules applied or to change the delimiter character.

 

 

 

Examples

 

WRITECSV PROD.NO, QTY TO SEQ.F ELSE STOP 'Write error'

 

This program fragment writes the contents of the PROD.NO and QTY variables as a CSV item to a file previously opened to file variable SEQ.F.

 

 

WRITECSV S<1>, S<2>, S<3> TO SEQ.F ELSE STOP 'Write error'

 

This program fragment writes the contents of fields 1 to 3 of S as CSV data to a file previously opened to file variable SEQ.F.

 

 

WRITECSV S TO SEQ.F ELSE STOP 'Write error'

 

If dynamic array S in the previous example had only three fields, this program fragment writes exactly the same data, treating each field as a separate CSV item.

 

 

See also:

CLOSESEQ, CREATE, CSVDQ(), CSV.MODE, DPARSE.CSV, NOBUF, OPENSEQ, PRINTCSV, READBLK, READCSV, READSEQ, WEOFSEQ, WRITESEQ, WRITEBLK, WRITESEQF