Skip to main content

Problem:

Application developers may need a special file handler which works with a particular type of file or device, but must keep all normal COBOL file handling functionality for other file types.

Resolution:

Attached is an example of a file handler written in COBOL which deals with Comma Separated Value (CSV) files.  CSV is a common data export format for Windows programs.  File organization is line sequential, (records delimited by platform-dependent newline character, X'0D0A' on Windows, X'0A' on Unix.)

The file handler in this example expects the file name to end with ".csv" to distinguish it from other line sequential files.  The CSV handler monitors all COBOL file activity while it passes to and from the normal file handler, but does nothing unless all of the following conditions are met:

Organization is line sequential

Current operation on the file is "read".

File name ends with ".csv"

The requested read operation is successful.

All those conditions can be tested in the data passed from a COBOL file i-o statement to the file handler, except for the file status.  The file status can be tested after the normal file handler operation and before control retuns to the application program.

When all conditions are met, the file handler separates the values in the record into individual array elements, removing the commas.  Commas protected inside double quotes, e.g. "Smith, John" are preserved and the quotes themselves removed.  A count of those array elements is returned.  A copy file, csv.cpy, is provided to use the structure.  The CSV array structure replaces the record read from disk.  The application does not receive the "raw" csv record from disk at all.

The file handler is packaged as a small but complete demonstration, including:

file handler source, csvfh.cbl

Demonstration application program, csvmain.cbl

CSV array copy file, csv.cpy

Sample data file, csvdata.csv

Compile and run script, csvdemo

To install the demonstration, download the attached tar file, csvdemo.tar to a Unix system on which Server Express is installed.  tar -xvf csvdemo.tar.  A subdirectory named "csv" contains the demonstration files.

To run the demonstration, cd csv and ./csvdemo.  The demonstration's animation can be varied by editing the csvdemo script as indicated in the comments.

Attachments:

csvdemo.tar

Old KB# 3701

#ServerExpress
#RMCOBOL
#COBOL
#netexpress
#AcuCobol