Open a CSV File

csvOpen

The csvOpen procedure opens a file on the IFS for processing CSV (comma separated values) files.

The csvOpen procedure can be used easily open a file on the IFS for processing by the csvWrite() procedure.

int4 IFSfile-handle =  csvOpen(
 szIFSFile           1024A    Const VARYING
 szFmtFileName         21A    Const OPTIONS(*NOPASS)
 szUserSpace           21A    Const OPTIONS(*NOPASS)
 bReplace                N    Const OPTIONS(*NOPASS)
 bHeaders                N    Const OPTIONS(*NOPASS:*OMIT)
 nReserved             10I 0  Const OPTIONS(*NOPASS:*OMIT)
 nCCSID                10I 0  Const OPTIONS(*NOPASS)
)

See also: csvWrite

Parameters

szIFSFile
[input VChar(512) const]  The name of the IFS file that will received comma separated values. The content of this file is replaced by this procedure, unless *OFF is passed on the bReplace parameter. If the file specified on this parameter does not exist, it will be created. The name may be qualified to a folder/directory as illustrated below:
 
 *...v....1....v....2....v....3....v....4....v....5....v....6....
'/home/cozzi/custmast.csv'
 
szFmtFileName
[input Char(21) const]  The name of the database file whose format is used when writing data to the IFS as comma separated values. The format file may be specified in either of the following syntax structures:
 *...v....1....v....2
'MYFILE    MYLIBR    '
 

Alternatively::

 *...v....1....v....2.
'MYLIB/MYFILE         '
 
szUserSpace  [ optional ] DFT('*')
[input Char(21) const]  The name of the user space that will be used to store the field definitions for the conversion to comma separated format. This parameter consists of two parts (1) in the first 10 positions, the name of the user space, and (2) in the second 10 positions, the name of the library in which the user space is located. For example:
 *...v....1....v....2
'MYSPACE   QTEMP     '
 

An alternative syntax is also supported for the user space parameter. The alternate syntax allows you to specify the user space name similar to that used by CL commands, that is it may be specified using qualified name syntax, as follows:

 *...v....1....v....2.
'QTEMP/MYSPACE        '
 

The qualified user space name is MYSPACE in the QTEMP library. Either syntax is acceptable.

 
The special value '*' (an asterisk) may be specified, which causes this procedure to use the default user space (created in the QTEMP library). Any data currently stored in the default user space is replaced. If the user space does not exist, the procedure will create it automatically.
 
bReplace  [ optional ] DFT(*ON)
[input Indy const]  Replace existing IFS file. If *ON or '1' is specified for this parameter, or the parameter is not specified, then if a file with the same name as the one specified on the szIFSFile parameter already exists, it will be deleted. If *OFF or '0' is specified for this parameter, data is written to the end of the existing file.
 
bHeaders  [ optional ] DFT(*ON)
[input Indy const]  Upon opening the output file, a row of headers is written to the file. The header row is derived from the list of field names from the DB2 file.
nReserved  [ optional ] DFT(0)
[input Int4 const]  Reserved for future use. You may pass *OMIT or 0 or you may simply pass nothing (assuming the nCCSID is not needed) for this parameter.
 
nCCSID  [ optional ] DFT(819)
[input Int4 const]  An optional CCSID to be used to set the CCSID of the file specified on the szIFSFile parameter. If this parameter is not specified or is specified as -1, the default PC ASCII CCSID of 819 is used. A value of 0 for this parameter indicates the CCSID of the job is used. Valid CCSID values for this parameter are 0, or 1 to 65535.  NOTE: The default CCSID of 819 may be changed by setting a new CCSID value in the XT_ASCIICCSID global import variable as follows:

     C                   eval      xt_ASCIICCSID = 37

Return Value

If the file is successfully open, the return value is an Int4 (10i0) value called a "file handle". This file handle should be saved and passed to calls to other CSVxxxxx procedures, such as csvWrite and csvClose.

Remarks

The output file is a file on the integrated file system (IFS) with the same name as the file specified on the szIFSFile parameter. The location is also the same as the folder assigned to the file, or the current directory is used.

Example

The following example uses csvOpen to create and open the output file named CUSTMAST.CSV on the IFS. It then reads each record from the customer database master file named CUSTMAST and uses csvWrite to convert the CUSTMAST input buffer into comma separated values and write those values to the IFS file. The final statement closes the CSV file.

     H BNDDIR('RPGLIB')

     FCUSTMAST  IF   E             DISK

 

     D hFile           S             10I 0

     D nBytes          S             10I 0

     D nBytesWritten   S             10U 0

     DCUSTDS         E DS                  EXTNAME(CUSTMAST)

      /COPY XTOOLS/QCPYSRC,CSV

.....C**n01..............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)

 

     C                   EVAL      *INLR = *ON