How to resolve the algorithm File input/output step by step in the COBOL programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm File input/output step by step in the COBOL programming language

Table of Contents

Problem Statement

Create a file called   "output.txt",   and place in it the contents of the file   "input.txt",   via an intermediate variable. In other words, your program will demonstrate:

Oneliners that skip the intermediate variable are of secondary interest — operating systems have copy commands for that.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm File input/output step by step in the COBOL programming language

Source code in the cobol programming language

       identification division.
       program-id. copyfile.
       environment division.
       input-output section.
       file-control.
           select input-file assign to "input.txt"
               organization sequential
           .
           select output-file assign to "output.txt"
               organization sequential
           .
       data division.
       file section.
       fd input-file.
       1 input-record pic x(80).
       fd output-file.
       1 output-record pic x(80).
       working-storage section.
       1 end-of-file-flag pic 9 value 0.
         88 eof value 1.
       1 text-line pic x(80).
       procedure division.
       begin.
           open input input-file
               output output-file
           perform read-input
           perform until eof
               write output-record from text-line
               perform read-input
           end-perform
           close input-file output-file
           stop run
           .
       read-input.
           read input-file into text-line
           at end
               set eof to true
           end-read
           .
       end program copyfile.


       IDENTIFICATION DIVISION.
       PROGRAM-ID. file-io.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT in-file ASSIGN "input.txt"
               ORGANIZATION LINE SEQUENTIAL.
             
           SELECT OPTIONAL out-file ASSIGN "output.txt"
               ORGANIZATION LINE SEQUENTIAL.

       DATA DIVISION.
       FILE SECTION.
       FD  in-file.
       01  in-line                 PIC X(256).

       FD  out-file.
       01  out-line                PIC X(256).
       
       PROCEDURE DIVISION.
       DECLARATIVES.
       in-file-error SECTION.
           USE AFTER ERROR ON in-file.
           DISPLAY "An error occurred while using input.txt."
           GOBACK
           .
       out-file-error SECTION.
           USE AFTER ERROR ON out-file.
           DISPLAY "An error occurred while using output.txt."
           GOBACK
           .
       END DECLARATIVES.

       mainline.
           OPEN INPUT in-file
           OPEN OUTPUT out-file

           PERFORM FOREVER
               READ in-file
                   AT END
                       EXIT PERFORM
               END-READ
               WRITE out-line FROM in-line
           END-PERFORM

           CLOSE in-file, out-file
           .


*> Originally from ACUCOBOL-GT
CALL "C$COPY" USING "input.txt", "output.txt", 0


*> Originally from Micro Focus COBOL
CALL "CBL_COPY_FILE" USING "input.txt", "output.txt"


  

You may also check:How to resolve the algorithm FizzBuzz step by step in the Fe programming language
You may also check:How to resolve the algorithm Long year step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Rosetta Code/Find unimplemented tasks step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Nth root step by step in the Ada programming language
You may also check:How to resolve the algorithm Guess the number step by step in the XBS programming language