Previous Topic Next topic Print topic


Example External Files

  1$set ans85 mf noosvs
  2*****************************************************
  3*
  4*    (C) 1997-2002 Micro Focus International Ltd.
  5*
  6*      EXTFILE.CBL
  7*
  8* This program demonstrates how to use EXTERNAL files
  9* It calls WRITEFIL to write some records to a data
 10* file and READFILE to read the same records back
 11* (without opening or closing the file between calls
 12* READFILE displays the output.
 13*
 14****************************************************
 15 identification division.
 16 program-id. extfile.
 17 environment division.
 18 input-output section.
 19 file-control.
 20 select finfile assign to "isamfil.dat"
 21  organization is indexed
 22  record key is fd-tran-date
 23  access mode is dynamic.
 24
 25 file section.
 26 fd finfile
 27  is external
 28   record contains 50 characters.
 29 01 fd-finfile-record.
 30   05 fd-tran-date   pic x(4).
 31   05 fd-with-or-dep   pic x(2).
 32   05 fd-amount    pic 9(5)v99.
 33
 34 procedure division.
 35 main-line.
 36   perform open-file
 37   perform write-to-the-file
 38   perform start-file
 39   perform read-the-file
 40   perform close-file
 41   stop run.
 42
 43 open-file.
 44   open i-o finfile.
 45
 46 start-file.
 47   move 1111 to fd-tran-date
 48   start finfile key = fd-tran-date.
 49
 50 write-to-the-file.
 51   call "writefil".
 52
 53 read-the-file.
 54   call "readfile".
 55
 56 close-file.
 57   close finfile.
 58 end program extfile.
 59*****************************************************
 60 identification division.
 61 program-id. readfile.
 62 environment division.
 63 input-output section.
 64 file-control.
 65 select finfile assign to "isamfil.dat"
 66   organization is indexed
 67   record key is fd-tran-date
 68   access mode is dynamic.
 69
 70 file section.
 71 fd finfile
 72   is external
 73   record contains 50 characters.
 74 01 fd-finfile-record.
 75   05 fd-tran-date   pic x(4).
 76   05 fd-with-or-dep  pic x(2).
 77   05 fd-amount    pic 9(5)v99.
 78
 79 working-storage section.
 80 01 ws-end-of-file   pic 9   value 0.
 81 01 ws-subtotal    pic s9(5)v99 value 0.
 82 01 ws-total     pic -(4)9.99.
 83
 84 procedure division.
 85 main-line.
 86   perform read-the-file.
 87   perform until ws-end-of-file = 1
 88   perform calculate-totals
 89   perform read-the-file
 90   end-perform.
 91   perform display-output.
 92   exit program.
 93   stop run.
 94
 95 read-the-file.
 96   read finfile next record at end
 97    move 1 to ws-end-of-file.
 98
 99 calculate-totals.
100   evaluate fd-with-or-dep
101    when "WI"
102     subtract fd-amount from ws-subtotal
103    when "DE"
104     add fd-amount to ws-subtotal
105   end-evaluate.
106
107 display-output.
108   move ws-subtotal to ws-total
109   display "account balance = ", ws-total.
110
111 end program readfile.
112****************************************************
113 identification division.
114 program-id. writefil.
115 environment division.
116 input-output section.
117 file-control.
118 select finfile assign to "isamfil.dat"
119   organization is indexed
120   record key is fd-tran-date
121   access mode is dynamic.
122
123 file section.
124 fd finfile
125  is external
126  record contains 50 characters.
127 01 fd-finfile-record.
128   05 fd-tran-date   pic x(4).
129   05 fd-with-or-dep   pic x(2).
130   05 fd-amount    pic 9(5)v99.
131
132 procedure division.
133 main-line.
134   perform write-records
135   exit program
136   stop run.
137
138 write-records.
139
140* write a WIthdrawal record
141   move 1111 to fd-tran-date.
142   move 'WI' to fd-with-or-dep.
143   move 23.55 to fd-amount.
144   write fd-finfile-record.
145
146* write a DEposit record
147   move 2222 to fd-tran-date.
148   move 'DE' to fd-with-or-dep.
149   move 123.55 to fd-amount.
150   write fd-finfile-record.
151
152 end program writefil.
Previous Topic Next topic Print topic