Menu

Problems recording records in random access file.

2020-07-13
2020-07-13
  • Augusto Manzano

    Augusto Manzano - 2020-07-13

    Hello again.
    I want to express my thanks to everyone for the help previously given to me. As you know, I am learning COBOL alone, reading books and manuals and taking some questions with you. Despite my efforts, there are times when I cannot resolve small details and I resort to some form of assistance.
    I am using the OpenCobolIDE 4.7.6 environment on Windows 10 with GnuCOBOL updated to version 3.1-rc1.0.
    The program to follow when in SEQUENTIAL mode reports success in recording the record in the created file, but when in RANDOM mode it informs that the record already exists and returns error code 24. What am I doing wrong?

           IDENTIFICATION DIVISION.
           PROGRAM-ID. FILEMNGR.
    
           ENVIRONMENT DIVISION.
           CONFIGURATION SECTION.
           INPUT-OUTPUT SECTION.
           FILE-CONTROL.
               SELECT FD-FILEIN ASSIGN TO "FILERAN.DTR"
               ORGANIZATION      IS RELATIVE
               ACCESS MODE       IS RANDOM
               RELATIVE KEY      IS WS-NUM-REG
               FILE STATUS       IS WS-GET-ERROR.
    
           DATA DIVISION.
           FILE SECTION.
           FD  FD-FILEIN.
           01  FS-CADFUN.
               05 F001-CODFUN  PIC X(09).
               05 F002-NAME    PIC X(40).
    
           WORKING-STORAGE SECTION.
           01  TB-VIRTUAL.
               05 TB-CADFUN.
                  10 LS-CODFUN PIC X(09).
                  10 LS-NAME   PIC X(40).
           77  WS-GET-ERROR    PIC XX.
           77  WS-NUM-REG      PIC 9(4).
           77  WS-ANS          PIC A VALUE "Y".
    
           PROCEDURE DIVISION.
               OPEN OUTPUT FD-FILEIN
               DISPLAY "DATA INPUT"
               DISPLAY "----------"
               IF WS-GET-ERROR = "00"
                  PERFORM FOREVER
                     DISPLAY X"0D"
                     DISPLAY "Code ID .: " WITH NO ADVANCING
                     ACCEPT LS-CODFUN
                     DISPLAY "Name ....: " WITH NO ADVANCING
                     ACCEPT LS-NAME
                     WRITE FS-CADFUN FROM TB-CADFUN
                       INVALID KEY
                          DISPLAY X"0D"
                          DISPLAY "ERROR CODE: " WS-GET-ERROR
                          DISPLAY "Existing record."
                       NOT INVALID KEY
                          DISPLAY X"0D"
                          DISPLAY "Successfully registered."
                     END-WRITE
                     DISPLAY X"0D"
                     DISPLAY "More data? (Y) for YES: " WITH NO ADVANCING
                     ACCEPT WS-ANS
                     IF FUNCTION UPPER-CASE(WS-ANS) NOT = "Y"
                        EXIT PERFORM
                     END-IF
                  END-PERFORM
               END-IF.
               CLOSE FD-FILEIN.
               STOP RUN.
    
           END PROGRAM FILEMNGR.
    
     
    • Vincent (Bryan) Coen

      Where are you creating the keyed value in WS-NUM-REG ?

      Vince

      On 13/07/2020 12:12, Augusto Manzano wrote:

      Hello again.
      I want to express my thanks to everyone for the help previously given
      to me. As you know, I am learning COBOL alone, reading books and
      manuals and taking some questions with you. Despite my efforts, there
      are times when I cannot resolve small details and I resort to some
      form of assistance.
      I am using the OpenCobolIDE 4.7.6 environment on Windows 10 with
      GnuCOBOL updated to version 3.1-rc1.0.
      The program to follow when in SEQUENTIAL mode reports success in
      recording the record in the created file, but when in RANDOM mode it
      informs that the record already exists and returns error code 24. What
      am I doing wrong?

          IDENTIFICATION  DIVISION.
          PROGRAM-ID.FILEMNGR.
      
          ENVIRONMENT  DIVISION.
          CONFIGURATION  SECTION.
          INPUT-OUTPUT  SECTION.
          FILE-CONTROL.
              SELECT  FD-FILEIN  ASSIGN  TO  "FILERAN.DTR"
              ORGANIZATION       IS  RELATIVE
              ACCESS  MODE        IS  RANDOM
              RELATIVE  KEY       IS  WS-NUM-REG
              FILE  STATUS        IS  WS-GET-ERROR.
      
          DATA  DIVISION.
          FILE  SECTION.
          FD   FD-FILEIN.
          01   FS-CADFUN.
              05  F001-CODFUN   PIC  X(09).
              05  F002-NAME     PIC  X(40).
      
          WORKING-STORAGE  SECTION.
          01   TB-VIRTUAL.
              05  TB-CADFUN.
                 10  LS-CODFUN  PIC  X(09).
                 10  LS-NAME    PIC  X(40).
          77   WS-GET-ERROR     PIC  XX.
          77   WS-NUM-REG       PIC  9(4).
          77   WS-ANS           PIC  A  VALUE  "Y".
      
          PROCEDURE  DIVISION.
              OPEN  OUTPUT  FD-FILEIN
              DISPLAY  "DATA INPUT"
              DISPLAY  "----------"
              IF  WS-GET-ERROR  =  "00"
                 PERFORM  FOREVER
                    DISPLAY  X"0D"
                    DISPLAY  "Code ID .: "  WITH  NO  ADVANCING
                    ACCEPT  LS-CODFUN
                    DISPLAY  "Name ....: "  WITH  NO  ADVANCING
                    ACCEPT  LS-NAME
                    WRITE  FS-CADFUN  FROM  TB-CADFUN
                      INVALID  KEY
                         DISPLAY  X"0D"
                         DISPLAY  "ERROR CODE: "  WS-GET-ERROR
                         DISPLAY  "Existing record."
                      NOT  INVALID  KEY
                         DISPLAY  X"0D"
                         DISPLAY  "Successfully registered."
                    END-WRITE
                    DISPLAY  X"0D"
                    DISPLAY  "More data? (Y) for YES: "  WITH  NO  ADVANCING
                    ACCEPT  WS-ANS
                    IF  FUNCTION  UPPER-CASE(WS-ANS)  NOT  =  "Y"
                       EXIT  PERFORM
                    END-IF
                 END-PERFORM
              END-IF.
              CLOSE  FD-FILEIN.
              STOP  RUN.
      
          END  PROGRAM  FILEMNGR.
      
       
      • Augusto Manzano

        Augusto Manzano - 2020-07-13

        Hi, thanks for the feedback.

        That's my question, too. I've been researching and looking for examples of use for days. And I can't find any information to clarify.

        I am learning COBOL on my own and sometimes I feel frustrated at not being able to see examples of using file access and manipulation techniques.

         
  • Vincent (Bryan) Coen

    Change definition to :

    77 WS-NUM-REG PIC 9(4) value zero.

    BEFORE writing the record out do :

    add 1 to WS-Num-Reg.

    That will fix it assuming you really want a random type key as against a ISAM one.

     
    • Augusto Manzano

      Augusto Manzano - 2020-07-13

      Hello, Vicent.

      Thank you very much for your time, assistance and clarification. It helped a lot.

      Here it is working.

       

Anonymous
Anonymous

Add attachments
Cancel