How do I make a tab delimited export file using Cobol?

Posted on 2007-10-13
Last Modified: 2013-11-25
How do I make a tab delimited export file using Cobol.  We have a program written by our previous programmer that gives me exactly what we want, but it is space delimited.  It exports several files and all I want to do is replace the single space FILLER with the tab character.   I haven't worked in Cobol for a long time so be specific!  (OK--dumb it down works too.)  

  FD  OUTPUT-FILE                                  
  01  OUTPUT-RECORD.                                
      03  FIELD-1                  PIC X(34).        
      03  FILLER                   PIC X(1).        
      03  FIELD-2                  PIC 9(4).        
      03  FILLER                   PIC X(1).        
      03  FIELD-3                  PIC X(30).        
      03  FILLER                   PIC X(1).        
      03  FIELD-4                  PIC X(02).        
Question by:annetje
    LVL 26

    Accepted Solution


    Since no one's picked this up yet...

    Without knowing the platform/compiler, it's hard to be certain of some details. Under an i5/OS compiler, you have a bit of difficulty because OUTPUT-RECORD has its fields defined directly beneath the FD entry for the file. The FD entry effectively described the file's I/O buffer rather than a separate WORKING-STORAGE memory area.

    This means that simple changes such as adding VALUE clauses to the FILLER items isn't very useful. Each new record image in the buffer will reinitialize the area and overlay the VALUEs.

    Ideally, the record image would be built in a WORKING-STORAGE 01-level item. That record image would be populated with your data and the FILLERs could have a fixed VALUE. Then you'd use a WRITE ... FROM to output the records.

    By doing the work outside of the buffer you'd take advantage of what the compiler can do to help you, at the cost of a tiny bit of extra coding for data definitions plus a slightly indirect I/O.

    Whenever possible, I try to avoid doing actual data manipulation in any buffer area. I always want to isolate anything that I do from what some underlying DBMS or operating system might do for buffering, double-buffering or what have you.

    In short, I'd move your 01-level OUTPUT-RECORD to under WORKING-STORAGE. I'd add VALUE '[tab]' to each appropriate FILLER, using whatever the [tab] character is one your platform, possibly a hex value. I'd create a new 01-level item under the FD perhaps named FD-OUTPUT-RECORD. And I'd change my WRITE to be WRITE FD-OUTPUT-RECORD FROM OUTPUT-RECORD.

    I'd also take care _not_ to clear the entire 01-level OUTPUT-RECORD but instead only reinitialize the individual 03-level field. Reinitializing the 01-level would wipe out my VALUEs.

    At least, that's how I'd do it given what appears to be your starting point. Technically, I probably wouldn't do it this way at all. I'd probably be using STRING to bring all of the elements together, and perhaps a variety of other technique changes.

    But that's beyond the scope of your question...


    Expert Comment

    You can get a tab delimited output file by using the Hex 09 value for your tab character.  In your Output-Record replace the filler by a variable name such as TAB-1.  See sample Output-Record below.  
       01  OUTPUT-RECORD.                                
          03  FIELD-1                  PIC X(34).        
          03  TAB-1                     PIC X(1).        
          03  FIELD-2                  PIC 9(4).        
          03  TAB-2                     PIC X(1).        
          03  FIELD-3                  PIC X(30).        
          03  TAB-3                     PIC X(1).        
          03  FIELD-4                  PIC X(02).    
    In your procedure division simply move the hex value 09 (zero nine) to the Tab-1, Tab-2, Tab-3 fields, like below.

    MOVE X09 TO TAB-1.
    MOVE X09 TO TAB-2.
    MOVE X09 TO TAB-3.

    The X in front of the 09 field indicates to COBOL that the value is a hex value.  Be sure to enclose the 09 in double quotes.

    This should give you a tab delimited file.

    Expert Comment

    The above comment at 2:25 shoud have the " character where the little boxes are.  copied and pasted the answer from a word document and the " character was repalced by the little boxes.  Sorry for any confusion.


    Expert Comment

    01  HEX-09-CREATE.
        02  FILLER        VALUE +09          PIC S9(02) COMP.
        02  HEX-09                           PIC X(01).
        02  FILLERXX                         PIC X(01).

    Put the above code in Working Storage, then in the Procedure Division simply
                      MOVE HEX-09                  TO SOME-FIELD.

    You can change the name to TAB if you wish and it works the same.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Outlook Free & Paid Tools
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now