Created On:  16 November 2012

Problem:

Customer creates a line sequential file that has different record lengths in it depending on the user running the application. They would like to use a single file description in their COBOL application to create these files so that they do not need a different description for each file.

The file name will be created dynamically and the records are different lengths. Also the records need to be padded out to the desired record length using spaces. How can this be done?

Resolution:

An example is attached to this article.

Here is what the program looks like:

      *----------------------------------------------------------------*
      *                      TESTVARYING                               
      *                                                                
      * This example demonstrates how to use a single file definition  
      * in COBOL to write to multiple physical files each with a diff- 
      * erent record length where trailing spaces are preserved in the 
      * record.                                                        
      *                                                                
      * In order to use variable length records we define the files    
      * with the RECORD IS VARYING IN SIZE DEPENDING ON ...PHRASE      
      *                                                                
      * We change the name of the physical file by assigning the file  
      * to an environment variable and then changing the contents of   
      * the environment variable prior to the file open.               
      *                                                                
      * In order to retain the trailing spaces out to the defined size 
      * of the record we need to specify the file handler option       
      * STRIPSPACE=OFF in an extfh.cfg file. In order to turn this on  
      * for just these files, we need to use the INTERNAL:myvariable   
      * as the filename.                                               
      *----------------------------------------------------------------*
       id division.
       program-id.  testvarying.
       environment division.
       input-output section.
           select my-file assign to external myvariable
                          organization is line sequential
                          file status is file-status.
       data division.
       file section.
       fd my-file
          recording mode is V
          record is varying in size
          from 10 to 20 characters depending on ws-record-size.
       01 my-record.
          05  my-data     pic x(20).
       working-storage section.
       01 ws-record-size  pic 9(3).
       01 ws-type1.
            05 data1      pic x(10).
       01 ws-type2.
            05 data2      pic x(20).
       01 file-status     pic x(2).
       procedure division.

          display "MYVARIABLE" upon environment-name
          display "myfile1.txt" upon environment-value
          open output my-file
          move "11111" to data1

          move 10 to ws-record-size
          move ws-type1 to my-record
          write my-record
          close my-file

          display "MYVARIABLE" upon environment-name
          display "myfile2.txt" upon environment-value
          open output my-file

          move "222" to data2
          move 20 to ws-record-size.
          move ws-type2 to my-record
          write my-record
          close my-file.