Write Comma Separated Values to IFS File

csvWrite

The csvWrite procedure writes a record to a file on the IFS in CSV (comma separated values) format.

The csvWrite procedure can be used to output comma-separated values to a file on the IFS. The conversion to CSV format is done by specifying a format file on which the buffer of data has been formatted. The IFS file must have been previously open with the csvOpen procedure or the open() Unix-style API.

 bytes-written = csvWrite(
  hIFSFile              10I 0  
  szBuffer           30720A    Const OPTIONS(*NOPASS:*OMIT:*VARSIZE)
  szFmtFileName         21A    Const OPTIONS(*NOPASS:*OMIT)
  szUserSpace           21A    Const OPTIONS(*NOPASS:*OMIT)
  bHeadersOnly            N    Const OPTIONS(*NOPASS:*OMIT)
)

See also: csvOpen, csvWriteHdr

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.
 
szBuffer
[input VChar(30720) const]  The raw data buffer that will be converted to CSV format an written to the IFS file. This buffer should be from a read to a DB2 file whose format is identical to the format file specified on the szFmtFile parameter. Typically the value passed on this parameter is a data structure that is externally described based on the same file specified on the szFmtFile parameter.
 
szFmtFile [ optional ]
[input Char(21) const]  The DB2 database file used as the format file to convert the raw buffer (szBuffer parameter) to comma separated values. This parameter is optional, and if not specified, defaults to the format file specified on the csvOpen procedure. Only one format file is retained in memory, however, so if you are processing multiple CSV files simultaneously you should specify the format file for this parameter. The file should be specified either by itself, or as a fully qualified OS/400 object name, as follows:
 
 *...v....1....v....2....v
'qgpl/custmast'
 
This parameter should be specified only when no file name was specified on the previous call to csvOpen. The first time you call csvWrite immediately following a csvOpen where the DB2 file name was not specified, you must specify the file name on this parameter. Subsequent calls to csvWrite do not require the file name to be supplied as the previously specified file name is saved between calls.
 
szUserSpace [ optional ]
[input Char(21) const]  The name of the user space into which the format file's fields are listed. This user space is used by the CSV routines to convert the raw input buffer to comma separated values. If a user space name was specified on the szUserSpace parameter of the csvOpen procedure, you do not need to specify a name for this parameter. Normally you do not need to supply a value for this parameter since it defaults to an internal user space that is created 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'
 
The first time you call csvWrite if szUserSpace is specified, the name is saved and is used on subsequent calls when szUserSpace is not specified. Normally you do not need to specify this parameter, but may do so to direct the user space to a known location. 
bHeadersOnly [ optional ] DFT(*OFF)
[input Indy const]  Specify *ON to cause the csvWrite procedure to output a row of comma separated field names. The szBuffer parameter is ignored when this parmater is *ON, and therfore the value *OMIT may be specified for the szBuffer parameter in this situation.  Normally you would use the csvWriteHdr helper procedure when only a row of "headers" is desired. In addition, the default behavior of the csvOpen procedure causes a row of headers to be written to the output IFS file, so normally this parameter is not needed.

Return Value

The return value is the number of bytes written to the record in the comma separated format. This includes the data from the file along with any quotes and commas.

Remarks

Normally, you only need to specify the file handle (hFile) and the output buffer when you call csvWrite unless you are processing several CSV file simultaneously. The other parameters are provided to override default behavior.

Example

The following example uses csvOpen to create and open the output file named CUSTMAST.CSV on the IFS. It then reads a record from the customer database master file named CUSTMAST and uses csvWrite to convert that record into CSV format and write it to the CUSTMAST.CSV file on the IFS. The final statement closes the CSV file.

Note that the hFile variable is returned by csvOpen, and passed to csvWrite and csvClose.

     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')

     C                   read      CustRec

     C                   dow       Not %EOF

     C                   eval      nBytes=csvWrite(hFile: CUSTDS)

     C                   eval      nBytesWritten= nBytesWritten + nBytes

     C                   Read      CustRec

     C                   enddo

     C                   callp     csvClose(hFile)

 

The csvOpen procedure opens the CUSTMAST.CSV file in the specified IFS folder/directory. The format file is CUSTMAST in QGPL. The user space used to store the field definitions defaults to the internal user space in QTEMP.

The csvWrite procedure converts the data read into the CUSTDS data structure (via the READ opcode) and converts that data to CSV format and writes it to the IFS file. The format file and user space are not specified and therefore default to what was specified on the previous csvOpen procedure.