Write a Header Row to a CSV File

csvWriteHdr

The csvWriteHdr procedure writes a record to a file on the IFS in CSV (comma separated values) format. This record contains a list of field names separated by the comma delimiter.

The csvWriteHdr procedure can be used generated a "header row" that is output to the IFS file.

bytes-written =  csvWriteHdr(
  hIFSFile              10I 0  
  szFmtFileName         21A    Const OPTIONS(*NOPASS)
  szUserSpace           21A    Const OPTIONS(*NOPASS)
)

See also: csvWrite

Parameters

hFile
[input int4 (10i0) ]  The file handle returned from a previous call to the csvOpen procedure. See example below for an illustration of how to declare a variable to be used as a file handle.
 
szFmtFile [ optional ]
[input Char(21) const]  The database format file whose field names are used to generate the headers. Each field name is added to an internal buffer and separated by a comma. That buffer is then written to the IFS file. The format file name should be specified either by itself, or as a fully qualified OS/400 object name, as follows:
 
 *...v....1....v....2....v
'qgpl/custmast'
 
szUserSpace [ optional ]
[input Char(21) const]  The name of the user space into which the format file's fields are listed. Normally you do not need to supply a value for this parameter since it defaults to user space name in the QTEMP library. If it is specified, it must be a fully qualified OS/400 object name, as follows:
 
 *...v....1....v....2....v
'QTEMP/XT_CSVFLD'
 

Return Value

The return value is the number of bytes written to the record in the comma separated value.

Remarks

Call this procedure if you did not specify the bHeaders(*ON) option on the csvOpen procedure to create and write a row of headers to your IFS file.

Normally you would specify bHeaders(*ON) during an open to the file with the csvOpen procedure and therefore not need to call this procedure.

Example

In following example, the csvOpen procedure opens the CUSTMAST.CSV file on the IFS for output and avoids writing a row of headers (the bHeaders parameter is *OFF). It then calls csvWriteHdr to generate and write a row of heads to the IFS file. The headers row is only written if the DB2/400 database file contains 1 or more records.

     FCUSTMAST  IF   E             DISK

 

     D hFile           S             10I 0

     D nBytes          S             10I 0

     D hBytesWritten   S             10U 0

     DCUSTDS         E DS                  EXTNAME(CUSTMAST)

 

.....CSRn01..............OpCode(ex)Extended-factor2++++++++++++++++++++++++++++++++++

     C                   eval      hFile = csvOpen('/home/cozzi/custmast.csv':

     C                                               'QGPL/CUSTMAST':'*':*ON:*OFF)

     C                   read      CustRec

     C                   if        Not %EOF

     C                   callp     csvWriteHdr(hFile:'QGPL/CUSTMAST')

     C                   dow       Not %EOF

     C                   eval      nBytes=csvWrite(hFile: CUSTDS)

     C                   eval      nBytesWritten = nBytesWritten + nBytes

     C                   Read      CustRec

     C                   enddo

     C                   endif

     C                   callp     csvClose(hFile)