Lame Fanello
Lame Fanello

Reputation: 615

Cobol: cannot find entry point of a text file

Hi i am learning cobol from tutorialpoints and every program from there works as i've tested them in OpenCobolIDE(some needed a little editing). Then i came across the File Handling chapter and in there the program had a lot of errors. I did manage to rewrite the program until it didn't show me any errors but it doesn't do anything.

Here's my code:

IDENTIFICATION DIVISION.
       PROGRAM-ID. HELLO.

       ENVIRONMENT DIVISION.
           INPUT-OUTPUT SECTION.
           FILE-CONTROL.
               SELECT STUDENT ASSIGN TO
               'C:\Cobol\FIle Handling\input.txt'
               ORGANIZATION IS INDEXED
               ACCESS IS RANDOM
               RECORD KEY IS STUDENT-ID
               FILE STATUS IS FS.

       DATA DIVISION.
           FILE SECTION.
           FD STUDENT.
               01 STUDENT-FILE.
               05 STUDENT-ID PIC 9(5).
               05 NAME PIC A(25).

           WORKING-STORAGE SECTION.
               01 WS-STUDENT-FILE.
               05 WS-STUDENT-ID PIC 9(5).
               05 WS-NAME PIC A(25).
               01 FS PIC 9(02).

       PROCEDURE DIVISION.
           OPEN I-O STUDENT.
               MOVE 20005 TO STUDENT-ID.
           READ STUDENT RECORD INTO WS-STUDENT-FILE
                   KEY IS STUDENT-ID
                   INVALID KEY DISPLAY 'Invalid Key'
                   NOT INVALID KEY DISPLAY WS-STUDENT-FILE
           END-READ.

           CLOSE STUDENT.
       STOP RUN.

This is the text file:

20003 Mohtashim M.
20004 Nishant Malik
20005 Amitabh Bachhan

The result should be the text:

20005 Amitabh Bachhan

Upvotes: 0

Views: 606

Answers (1)

Scott Nelson
Scott Nelson

Reputation: 616

It's doing something: It's reading the file. But that's all; you didn't ask for it to display or do anything else beyond reading the record into memory. You might want to look at using the DISPLAY statement or maybe create another file to write the output to.

Might I make a couple of suggestions?

In modern COBOL, stylistically, you don't put a period after everything in the procedure division -- you only put it in where it is necessary. For example:

   PROCEDURE DIVISION.
       OPEN I-O STUDENT
       MOVE 20005 TO STUDENT-ID
       READ STUDENT RECORD INTO WS-STUDENT-FILE
          KEY IS STUDENT-ID
          INVALID KEY DISPLAY 'Invalid Key'
          NOT INVALID KEY DISPLAY WS-STUDENT-FILE
       END-READ

       CLOSE STUDENT
       STOP RUN
       .

Although the compiler doesn't care about spaces and returns, if I were you, I'd try to indent my code a bit better (I like how I indented the above :-) ). It's up to you and a lot of people like to do it differently, but if you are consistent you can spot problems that might sneak through your code.

Edit: I didn't notice that you were reading with a key from a text file. So, either you need to:

  1. read from a pre-built indexed file, or
  2. read the file sequentially and search for what you want by comparing what you read for the student id you wanted.

Upvotes: 2

Related Questions