我正在编码例程,例如:

READ-A.
       READ FILE-A
           AT END
             MOVE 1 TO EOF-A
           NOT AT END
             ADD 1 TO CN-READ-A
       END-READ.
F-READ-A. EXIT.

读取多个文件,我想知道是否有一种方法可以编写一个能够从变量中读取文件名的例程,这样我就不必为每个文件编写相同的代码。谢谢!

最佳答案

如上所述的一种解决方案是使用多个程序或嵌套程序,为此
我在下面包含了一个示例,即解决方案 1。

另一个解决方案是 COBOL 类,这可能不符合您的喜好,但我喜欢它们,因此我提供了一个示例,即解决方案 2。

解决方案1:


   program-id. Program1.

   file-control.
       select file-a assign to myfile
           organization is line sequential.

   data division.
   fd file-a.
   01 file-a-line      pic x(80).

   working-storage section.
   01 EOF-A            pic 9 value 0.
   linkage section.
   01 lk-filename      pic x(128).
   01 CN-READ-A        pic 9(9).
   procedure division using lk-filename
                            CN-READ-A.

       move lk-filename to myfile
       open input file-a

       perform READ-A until EOF-A equals 1
       close file-a
       goback.

       READ-A.
       READ FILE-A
           AT END
             MOVE 1 TO EOF-A
           NOT AT END
             ADD 1 TO CN-READ-A
       END-READ.
       F-READ-A.
       EXIT.


   end program Program1.

解决方案2


   procedure division.
       set file-counter to new type FileLineCounter("d:\rts_win32.txt")
       display file-counter::LineCount
       stop run
   end program TestProgram.


   file-control.
       select file-a assign to myfile
           organization is line sequential.

   data division.
   fd file-a.
   01 file-a-line      pic x(80).

   working-storage section.

   01 cn-read-a binary-long property as "LineCount".

   method-id New.
   01 EOF-A            pic 9 value 0.
   procedure division using by value filename as string.

       set myfile to filename
       open input file-a

       perform READ-A until EOF-A equals 1
       close file-a
       goback.

       READ-A.
       READ FILE-A
           AT END
             MOVE 1 TO EOF-A
           NOT AT END
             ADD 1 TO CN-READ-A
       END-READ.
       F-READ-A.
       EXIT.

   end method.

   end class.

关于cobol - 有没有办法在 COBOL 中参数化函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4819272/

10-13 00:12