2013-03-02 36 views
2

我有三個VSAM文件。一個用於客戶,視頻和租賃。在這些VSAM文件中,每個客戶,視頻和租賃都有一個ID。下面是客戶文件的例子:編寫一個從單獨的vsam文件中提取信息的Cobol程序遇到了困難

300,鮑勃,315-123-1414

301,瑞安,315-213-2617

302,史密斯,315-123-1234

303,Roberta,212-125-1234

視頻文件和出租文件的設置方法相同。

我需要做的是編寫一個cobol程序,它將讀取這些VSAM文件,然後列出客戶,視頻以及客戶租用哪些視頻。我計劃通過爲每位顧客提供一個身份證號碼,然後將該身份證號碼與租借的視頻進行比較。

的COBOL代碼我迄今爲止低於:

ID DIVISION. 
    PROGRAM-ID. PROG3 

    ENVIRONMENT DIVISION. 
    CONFIGURATION SECTION. 
    SOURCE-COMPUTER. IBM-Z10. 
    OBJECT-COMPUTER. IBM-Z10. 
    INPUT-OUTPUT SECTION. 
    FILE-CONTROL. 
     SELECT CUR-FILE ASSIGN TO MYFILE1 
     ORGANIZATION INDEXED ACCESS IS SEQUENTIAL 
     RECORD KEY IS EMP-NO FILE STATUS IS F13. 

    DATA DIVISION. 
    FILE SECTION. 
    FD CUR-FILE 
     RECORD CONTAINS 80 CHARACTERS 
     DATA RECORD IS CUR-REC. 
     01 CUR-REC. 
     02 EMP-NO  PIC X(6). 
     02 EMP-NAME PIC X(24). 
     02 EMP-ADDRESS PIC X(50). 
     WORKING-STORAGE SECTION. 
     77 F13 PIC 99 VALUE ZEROS. 


     PROCEDURE DIVISION. 
     0001-MAIN. 
      DISPLAY ' I M IN MAIN '. 
      DISPLAY ' I M IN MAIN '. 
      OPEN OUTPUT CUR-FILE. IF F13 = 00 
      DISPLAY ' I M OPENED SUCCESSFULLY ' ELSE 
      DISPLAY 'OPEN-ERROR ' F13 STOP RUN. 
      MOVE '822655' TO EMP-NO. 
      DISPLAY EMP-NO. 
      MOVE 'MUSADDIQ USMAN' TO EMP-NAME. 
      MOVE 'P-5/01 STEEL TOWN' TO EMP-ADDRESS. 
      DISPLAY CUR-REC. 
      WRITE CUR-REC. 
      IF F13 = 00 DISPLAY 'WRITE SUCCESSFUL' ELSE 
      DISPLAY 'WRITE ERROR ' F13 STOP RUN. 
      CLOSE CUR-FILE. 
      STOP RUN. 

下面我將讀出的程序,我認爲這將有助於你幫我希望:

ID DIVISION. 
    PROGRAM-ID. RDVSAM. 

    ENVIRONMENT DIVISION. 
    CONFIGURATION SECTION. 
    SOURCE-COMPUTER. IBM-Z10. 
    OBJECT-COMPUTER. IBM-Z10. 
    INPUT-OUTPUT SECTION. 
    FILE-CONTROL. 
     SELECT CUR-FILE ASSIGN TO MYFILE1 
     ORGANIZATION INDEXED ACCESS IS SEQUENTIAL 
     RECORD KEY IS EMP-NO FILE STATUS IS F13. 

    DATA DIVISION. 
    FILE SECTION. 
    FD CUR-FILE 
     RECORD CONTAINS 80 CHARACTERS 
     DATA RECORD IS CUR-REC. 
     01 CUR-REC. 
     02 EMP-NO PIC 9(6). 
     02 EMP-NAME PIC X(24). 
     02 EMP-ADDRESS PIC X(50). 
    WORKING-STORAGE SECTION. 
    77 F13 PIC 99 VALUE ZEROS. 

    PROCEDURE DIVISION. 
    0001-MAIN. 
     OPEN INPUT CUR-FILE. IF F13 = 00 
     DISPLAY ' I M OPENED SUCCESSFULLY ' ELSE 
     DISPLAY 'OPEN-ERROR ' F13 STOP RUN. 
    REAd-FILE. 
     READ CUR-FILE AT END GO TO CLOSE-UP. 
     DISPLAY EMP-NO ' ' EMP-NAME ' ' EMP-ADDRESS. 
     GO TO READ-FILE. 
    CLOSE-UP. 
     CLOSE CUR-FILE. 
     STOP RUN. 

我堅持和唐不知道如何列出所有的信息。

謝謝

+1

你說你需要一個程序來讀取這三個文件,但是你會顯示一個程序將一條記錄寫入其中一個文件。你試圖加載一些數據的程序有問題嗎?或者你想知道如何處理這三個文件? – 2013-03-02 23:44:06

+0

對不起,當我開始工作時,我沒有完全理解它。我基本上沒有弄明白,但是你發佈的程序正確。我有三個單獨的Vsam文件,這些文件是由我添加到我的主文章中的I程序讀取的。我發佈的程序寫了我添加的第二個程序讀取的內容。但是,我無法讓該程序一次讀取所有3個vsam文件,然後寫入數據。 – user1486774 2013-03-03 00:59:43

回答

2

超過通過索引的文件掃描的例子見http://opencobol.add1tocobol.com/#does-opencobol-support-isam,也許http://opencobol.add1tocobol.com/#relative(不同的文件組織)

注意:這只是提示。查看開始並閱讀下一個線索

OCOBOL >>SOURCE FORMAT IS FIXED 
     *> *************************************************************** 
     *><* ================ 
     *><* indexing example 
     *><* ================ 
     *><* :Author: Brian Tiffin 
     *><* :Date:  17-Feb-2009 
     *><* :Purpose: Fun with Indexed IO routines 
     *><* :Tectonics: cobc -x indexing.cob 
     *> *************************************************************** 
     identification division. 
     program-id. indexing. 

     environment division. 
     configuration section. 

     input-output section. 
     file-control. 
      select optional indexing 
      assign to "indexing.dat" 
      organization is indexed 
      access mode is dynamic 
      record key is keyfield of indexing-record 
      alternate record key is splitkey of indexing-record 
       with duplicates 
      . 

     *> ** OpenCOBOL does not yet support split keys ** 
     *> alternate record key is newkey 
     *>  source is first-part of indexing-record 
     *>    last-part of indexing-record 
     *>  with duplicates 

     data division. 
     file section. 
     fd indexing. 
     01 indexing-record. 
      03 keyfield   pic x(8). 
      03 splitkey. 
      05 first-part  pic 99. 
      05 middle-part pic x. 
      05 last-part  pic 99. 
      03 data-part   pic x(54). 

     working-storage section. 
     01 display-record. 
      03 filler   pic x(4) value spaces. 
      03 keyfield   pic x(8). 
      03 filler   pic xx value spaces. 
      03 splitkey. 
      05 first-part  pic z9. 
      05 filler   pic x  value space. 
      05 middle-part pic x. 
      05 filler   pic xx value all "+". 
      05 last-part  pic z9. 
      03 filler   pic x(4) value all "-". 
      03 data-part   pic x(54). 

     *> control break 
     01 oldkey    pic 99x99. 

     *> In a real app this should well be two separate flags 
     01 control-flag   pic x. 
      88 no-more-duplicates   value high-value 
      when set to false is    low-value. 
      88 no-more-records    value high-value 
      when set to false is    low-value. 

     *> *************************************************************** 
     procedure division. 

     *> Open optional index file for read write 
     open i-o indexing 

     *> populate a sample database 
     move "1234567800a01some 12345678 data here" to indexing-record 
     perform write-indexing-record 
     move "0a01some 87654321 data here" to indexing-record 
     perform write-indexing-record 
     move "1234876500a01some 12348765 data here" to indexing-record 
     perform write-indexing-record 
     move "8765123400a01some 87651234 data here" to indexing-record 
     perform write-indexing-record 

     move "1234567900b02some 12345679 data here" to indexing-record 
     perform write-indexing-record 
     move "0b02some 97654321 data here" to indexing-record 
     perform write-indexing-record 
     move "1234976500b02some 12349765 data here" to indexing-record 
     perform write-indexing-record 
     move "9765123400b02some 97651234 data here" to indexing-record 
     perform write-indexing-record 

     move "1234568900c13some 12345689 data here" to indexing-record 
     perform write-indexing-record 
     move "0c13some 98654321 data here" to indexing-record 
     perform write-indexing-record 
     move "1234986500c13some 12349865 data here" to indexing-record 
     perform write-indexing-record 
     move "9865123400c13some 98651234 data here" to indexing-record 
     perform write-indexing-record 

     *> close it ... not necessary, but for the example 
     close indexing 

     *> clear the record space for this example 
     move spaces to indexing-record 

     *> open the data file again 
     open i-o indexing 

     *> read all the duplicate 00b02 keys 
     move 00 to first-part of indexing-record 
     move "b" to middle-part of indexing-record 
     move 02 to last-part of indexing-record 

     *> using read key and then next key/last key compare 
     set no-more-duplicates to false 
     perform read-indexing-record 
     perform read-next-record 
      until no-more-duplicates 

     *> read by key of reference ... the cool stuff 
     move 00 to first-part of indexing-record 
     move "a" to middle-part of indexing-record 
     move 02 to last-part of indexing-record 

     *> using start and read next 
     set no-more-records to false 
     perform start-at-key 
     perform read-next-by-key 
      until no-more-records 

     *> read by primary key of reference 
     move "87654321" to keyfield of indexing-record 

     *> 
     set no-more-records to false 
     perform start-prime-key 
     perform read-previous-by-key 
      until no-more-records 

     *> and with that we are done with indexing sample 
     close indexing 

     goback. 
     *> *************************************************************** 

     *><* Write paragraph 
     write-indexing-record. 
     write indexing-record 
      invalid key 
       display 
        "rewrite key: " keyfield of indexing-record 
       end-display 
        rewrite indexing-record 
         invalid key 
          display 
           "really bad key: " 
           keyfield of indexing-record 
          end-display 
        end-rewrite 
     end-write 
     . 

     *><* read by alternate key paragraph 
     read-indexing-record. 
      display "Reading: " splitkey of indexing-record end-display 
      read indexing key is splitkey of indexing-record 
     invalid key 
      display 
       "bad read key: " splitkey of indexing-record 
      end-display 
       set no-more-duplicates to true 
      end-read 
     . 

     *><* read next sequential paragraph 
     read-next-record. 
      move corresponding indexing-record to display-record 
      display display-record end-display 
      move splitkey of indexing-record to oldkey 

      read indexing next record 
       at end set no-more-duplicates to true 
       not at end 
        if oldkey not equal splitkey of indexing-record 
         set no-more-duplicates to true 
        end-if 
      end-read 
     . 

     *><* start primary key of reference paragraph 
     start-prime-key. 
      display "Prime < " keyfield of indexing-record end-display 
      start indexing 
       key is less than 
        keyfield of indexing-record 
       invalid key 
        display 
         "bad start: " keyfield of indexing-record 
        end-display 
        set no-more-records to true 
       not invalid key 
        read indexing previous record 
         at end set no-more-records to true 
        end-read 
      end-start 
     . 

     *><* read previous by key of reference paragraph 
     read-previous-by-key. 
      move corresponding indexing-record to display-record 
      display display-record end-display 

      read indexing previous record 
       at end set no-more-records to true 
      end-read 
     . 
     *><* start alternate key of reference paragraph 
     start-at-key. 
      display "Seeking >= " splitkey of indexing-record end-display 
      start indexing 
       key is greater than or equal to 
        splitkey of indexing-record 
       invalid key 
        display 
         "bad start: " splitkey of indexing-record 
        end-display 
        set no-more-records to true 
       not invalid key 
        read indexing next record 
         at end set no-more-records to true 
        end-read 
      end-start 
     . 

     *><* read next by key of reference paragraph 
     read-next-by-key. 
      move corresponding indexing-record to display-record 
      display display-record end-display 

      read indexing next record 
       at end set no-more-records to true 
      end-read 
     . 
     end program indexing. 
1

好吧,我認爲IBM Cobol手冊,我假設IBM因VSAM而在網上提供。爲您的編譯器版本(這是每個編譯列表的首頁)搜索「語言參考」和「編程指南」。

您需要使用KEY讀取才能找到單個記錄,或者如果您有一組記錄,則還可以使用READ/KEY和START和READ NEXT進行查找。如果您只有部分密鑰,則使用START和READ NEXT。 Brian已經提出過這個問題。

你在學習Cobol嗎?您應該將工作放在避免轉到並以不同方式格式化您的程序,每行只有一個動詞,可以幫助您進一步減少句號/句點(以Brian的例子爲例,僅在「完全停止」行結尾每個段落,這是除了段落/ SECTIONS和PROCEDURE DIVISION聲明本身之外,您需要的最全面的站點,並且如果您沒有段落/ SECTIONS,則結束程序。

有一件事你不應該從Brian's代碼,這是一個「環境」的東西在大型機上,如果你在文件打開之前或關閉之後訪問記錄區域,你會得到一個S0C4 abend(除非它是一個只有APPLY WRITE的QSAM文件,直接或通過AWO編譯選項)

有沒有必要設置記錄 - 在閱讀之前將其初始值初始化。

+0

謝謝你的票據比爾。我會着手糾正這個例子。 – 2013-03-03 08:33:20