读取并跳转文件中的第一行和其他行

时间:2013-01-28 20:06:46

标签: cobol gnucobol

如何使用以下结构读取.dat文件:(A = ALPHANUMERIC&& N = NUM​​ERIC)

0AAAAAAAANNNN (233 BLANK SPACES ) 999999 ( SEQUENTIAL NUMBER ONE BY ONE )
1NNNNNNNNNNNNAAAAAAAAAAAAAAAAAAA (194 BLANK SPACES) 999999 (SEQUENTIAL NUMBER ONE BY ONE)
2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA (194 BLANK SPACES) 999999 (SEQUENTIAL NUMBER ONE BY ONE)
1NNNNNNNNNNNNAAAAAAAAAAAAAAAAAAA (194 BLANK SPACES) 999999 (SEQUENTIAL NUMBER ONE BY ONE)
2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA (194 BLANK SPACES) 999999 (SEQUENTIAL NUMBER ONE BY ONE)
1NNNNNNNNNNNNAAAAAAAAAAAAAAAAAAA (194 BLANK SPACES) 999999 (SEQUENTIAL NUMBER ONE BY ONE)
2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA (194 BLANK SPACES) 999999 (SEQUENTIAL NUMBER ONE BY ONE)
1NNNNNNNNNNNNAAAAAAAAAAAAAAAAAAA (194 BLANK SPACES) 999999 (SEQUENTIAL NUMBER ONE BY ONE)
2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA (194 BLANK SPACES) 999999 (SEQUENTIAL NUMBER ONE BY ONE)
9 (245 BLANK SPACES) 999999 (SEQUENTIAL NUMBER ONE BY ONE)

所以,我知道,我如何制作一个程序来用C / C ++或C#来读取它,但是,我试着在Cobol中制作,只是为了学习....

但是,我不知道用这个样式打开文件需要用什么命令(我只知道:

ORGANIZATION IS LINE SEQUENTIAL.

我认为,存在另一个命令,用随机指令打开......我不知道...... 那么,顺便说一句,我如何打开文件并阅读信息?

(我只需按时读取第1行,并且,我需要读取第2行和第3行总是在... 4和5& 6和7&& 8和9 ... 。) 而且,我想用DISPLAY显示这些信息(仅供学习) 谢谢:))

3 个答案:

答案 0 :(得分:2)

你的FD下面有这样的东西:

01  INPUT-RECORD.
    05  IR-RECORD-TYPE PIC X.
        88  INPUT-RECORD-IS-HEADER VALUE '0'.
        88  INPUT-RECORD-IS-DATA1 VALUE '1'.
        88  INPUT-RECORD-IS-DATA2 VALUE '2'.
        88  INPUT-RECORD-IS-TRAILER VALUE '9'.
    05  FILLER PIC X(whatever).

对于记录分隔符,您可能需要一个“尾随”字节,我不知道,并且您必须对长度进行排序,因为它们似乎有所不同。

这些在工作存储中:

01  INPUT-RECORD-HEADER.
    05  IRH-RECORD-TYPE PIC X.
    05  IRH-ITEM1 PIC X(8).
    05  IRH-ITEM2 PIC 9(4).
    05  FILLER PIC X(233).
    05  IRH-SEQUENCE PIC X(6)
01  INPUT-RECORD-DATA1.
    05  IRD1-RECORD-TYPE PIC X.
    05  IRD1-ITEM1 PIC 9(14).
    05  IRD1-ITEM1 PIC X(19).
    05  FILLER PIC X(194).
    05  IRD1-SEQUENCE PIC X(6)
01  INPUT-RECORD-DATA2.
    05  IRD2-RECORD-TYPE PIC X.
    05  IRD2-ITEM1 PIC X(33).
    05  FILLER PIC X(194).
    05  IRD2-SEQUENCE PIC X(6)
01  INPUT-RECORD-TRAILER.
    05  IRT-RECORD-TYPE PIC X.
    05  FILLER PIC X(245).
    05  IRT-SEQUENCE PIC X(6).

您必须一次读取一条记录。识别它。将其置于正确的W-S定义中。当您读取“2”时,您可以处理与“2”一起存储的“1”。

我的数据名不是很好,因为我不知道你的数据是什么。此外,我没有“格式化”定义,这将使它们在您执行时更具可读性。

答案 1 :(得分:1)

对于OpenCOBOL,以下是/标准输出过滤器程序中的示例标准:

   >>SOURCE FORMAT IS FIXED
  *> ***************************************************************
  *><* ===========
  *><* filter
  *><* ===========
  *><* :Author:    Brian Tiffin
  *><* :Date:      20090207
  *><* :Purpose:   Standard IO filters
  *><* :Tectonics: cobc -x filter.cob
  *> ***************************************************************
   identification division.
   program-id. filter.

   environment division.
   configuration section.

   input-output section.
   file-control.
       select standard-input assign to keyboard.
       select standard-output assign to display.

   data division.
   file section.
   fd standard-input.
       01 stdin-record     pic x(32768).
   fd standard-output.
       01 stdout-record    pic x(32768).

   working-storage section.
   01  file-status         pic x  value space.
       88 end-of-file             value high-value
          when set to false is          low-value.

  *> ***************************************************************
   procedure division.
   main section.
   00-main.

   perform 01-open

   perform 01-read

   perform
        until end-of-file
            perform 01-transform
            perform 01-write
            perform 01-read
   end-perform
   .

   00-leave.
   perform 01-close
   .

   goback.
  *> end main

   support section.
   01-open.
   open input standard-input
   open output standard-output
   .

   01-read.
   read standard-input
        at end set end-of-file to true
   end-read
   .

  *> All changes here
   01-transform.
   move stdin-record to stdout-record
   .
  *>

   01-write.
   write stdout-record end-write
   .

   01-close.
       close standard-input
       close standard-output
   .

   end program filter.
  *><*
  *><* Last Update: dd-Mmm-yyyy

这是一个使用LINAGE的演示,它恰好在文本文件中读取。

  *****************************************************************
  * Example of LINAGE File Descriptor
  * Author: Brian Tiffin
  * Date:   10-July-2008
  * Tectonics: $ cocb -x linage-demo.cob
  *            $ ./linage-demo <filename ["linage-demo.cob"]>
  *            $ cat -n mini-report
  *****************************************************************
   IDENTIFICATION DIVISION.
   PROGRAM-ID. linage-demo.

   ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.
      select optional data-file assign to file-name
          organization is line sequential
          file status is data-file-status.
      select mini-report assign to "mini-report".

   DATA DIVISION.
   FILE SECTION.
   FD  data-file.
   01  data-record.
      88 endofdata        value high-values.
      02 data-line        pic x(80).
   FD  mini-report
      linage is 16 lines
          with footing at 15
          lines at top 2
          lines at bottom 2.
   01  report-line         pic x(80).

   WORKING-STORAGE SECTION.
   01  command-arguments   pic x(1024).
   01  file-name           pic x(160).
   01  data-file-status    pic 99.
   01  lc                  pic 99.
   01  report-line-blank.
      02 filler           pic x(18) value all "*".
      02 filler           pic x(05) value spaces.
      02 filler           pic x(34)
          VALUE "THIS PAGE INTENTIONALLY LEFT BLANK".
      02 filler           pic x(05) value spaces.
      02 filler           pic x(18) value all "*".
   01  report-line-data.
      02 body-tag         pic 9(6).
      02 line-3           pic x(74).
   01  report-line-header.
      02 filler           pic x(6) VALUE "PAGE: ".
      02 page-no          pic 9999.
      02 filler           pic x(24).
      02 filler           pic x(5) VALUE " LC: ".
      02 header-tag       pic 9(6).
      02 filler           pic x(23).
      02 filler           pic x(6) VALUE "DATE: ".
      02 page-date        pic x(6).

   01  page-count          pic 9999.

   PROCEDURE DIVISION.

   accept command-arguments from command-line end-accept.
   string
      command-arguments delimited by space
      into file-name
   end-string.
   if file-name equal spaces
      move "linage-demo.cob" to file-name
   end-if.

   open input data-file.
   read data-file
      at end
          display
               "File: " function trim(file-name) " open error"
          end-display
          go to early-exit
   end-read.

   open output mini-report.

   write report-line
      from report-line-blank
   end-write.

   move 1 to page-count.
   accept page-date from date end-accept.
   move page-count to page-no.
   write report-line
      from report-line-header
      after advancing page
   end-write.

   perform readwrite-loop until endofdata.

   display
      "Normal termination, file name: "
      function trim(file-name)
      " ending status: "
      data-file-status
   end-display.
   close mini-report.

  * Goto considered harmful?  Bah!  :)
   early-exit.
   close data-file.
   exit program.
   stop run.

  ****************************************************************
   readwrite-loop.
   move data-record to report-line-data
   move linage-counter to body-tag
   write report-line from report-line-data
      end-of-page
          add 1 to page-count end-add
          move page-count to page-no
          move linage-counter to header-tag
          write report-line from report-line-header
              after advancing page
          end-write
   end-write
   read data-file
      at end set endofdata to true
   end-read
   .

  *****************************************************************
  * Commentary
  * LINAGE is set at a 20 line logical page
  *  16 body lines
  *   2 top lines
  *   A footer line at 15 (inside the body count)
  *   2 bottom lines
  * Build with:
  * $ cobc -x -Wall -Wtruncate linage-demo.cob
  * Evaluate with:
  * $ ./linage-demo
  * This will read in linage-demo.cob and produce mini-report
  * $ cat -n mini-report
  *****************************************************************
   END PROGRAM linage-demo.

对于这些样本,以及Gilbert的回答,你应该有足够的解决问题的方法,但需要注意的是这些例子对正确的错误处理很害羞,所以要小心这是家庭作业还是付费作业。有关标准输入/输出或文件名的示例(取决于命令行参数(或缺少),请参阅OpenCOBOL FAQ中的ocdoc.cob程序。

Offtopic:ocdoc.cob本身的ocdoc传递输出可以在http://opencobol.add1tocobol.com/ocdoc.html看到(为什么要提到它?用于Pygments的COBOL词典荧光笔刚被接受为main。任何在1.6版之后拉出的Pygments将允许对于COBOL(无上下文)词汇突出显示。)

答案 2 :(得分:0)

您编写了一个读取文件的普通Cobol程序。

记录的第一个字节(字符)为0,1,2或9。

为4种记录类型中的每一种定义工作存储区域(01级别)。然后,在您阅读记录后,将其从输入区域移动到相应的工作存储区域以进行记录。

然后,您可以从4个工作存储区域之一处理记录的方式。