1

我如何读取具有类似结构的 .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 ...)而且,我想证明显示信息(仅供学习)谢谢:)

4

3 回答 3

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).

您必须一次阅读每条记录。识别它。将其放入正确的 WS 定义中。当您读取“2”时,您可以处理与“2”一起存储的“1”。

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

于 2013-01-28T22:39:32.220 回答
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 程序。

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

于 2013-01-28T20:42:24.847 回答
0

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

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

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

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

于 2013-01-28T20:14:15.200 回答