Overclock.net banner

1 - 5 of 5 Posts

·
Robot
Joined
·
865 Posts
Discussion Starter #1
I have an assignment for my COBOL programming class that requires me to read input from a file and display it. I'm pretty confused. Anyways I typed in some code that most likely won't even do what I want but I still need it to run. My two files are WORKSHEET2.dat and WORKSHEETOUT2.dat with the former containing the input to display.

Code:

Code:
       IDENTIFICATION DIVISION.
       PROGRAM-ID. FILE-DISPLAY.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT WORKSHEET2 ASSIGN TO WORKSHEETOUT2.
       DATA DIVISION.
       FILE SECTION.
       FD WORKSHEET2.
       01 NAME1 PIC X(20).
       WORKING-STORAGE SECTION.
       PROCEDURE DIVISION.
       BEGIN.
       OPEN INPUT WORKSHEET2.
       READ WORKSHEET2
       END-READ
       DISPLAY NAME1.
       CLOSE WORKSHEET2
       STOP RUN.
It compiles fine for me but whenever I try to run the program it says "4 Illegal File Name" and highlights line 14, the line right below the BEGIN. command. Any advice would be helpful, thanks.
 

·
Registered
Joined
·
1 Posts
I normally assign it to a literal/filename, so using OpenCOBOL or Micro Focus COBOL..

stephen-gennards-macbook:fd spg$ cat fd.cbl
IDENTIFICATION DIVISION.
PROGRAM-ID. FILE-DISPLAY.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT WORKSHEET2 ASSIGN TO "WORKSHEETOUT2.dat".
DATA DIVISION.
FILE SECTION.
FD WORKSHEET2.
01 NAME1 PIC X(20).
WORKING-STORAGE SECTION.
PROCEDURE DIVISION.
BEGIN.
OPEN INPUT WORKSHEET2.
READ WORKSHEET2
END-READ
DISPLAY NAME1.
CLOSE WORKSHEET2
STOP RUN.
stephen-gennards-macbook:fd spg$ cat WORKSHEETOUT2.dat
Hello World
stephen-gennards-macbook:fd spg$ ./fd
Hello World
 

·
Robot
Joined
·
865 Posts
Discussion Starter #3
Quote:

Originally Posted by spgennard View Post
I normally assign it to a literal/filename, so using OpenCOBOL or Micro Focus COBOL..

stephen-gennards-macbook:fd spg$ cat fd.cbl
IDENTIFICATION DIVISION.
PROGRAM-ID. FILE-DISPLAY.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT WORKSHEET2 ASSIGN TO "WORKSHEETOUT2.dat".
DATA DIVISION.
FILE SECTION.
FD WORKSHEET2.
01 NAME1 PIC X(20).
WORKING-STORAGE SECTION.
PROCEDURE DIVISION.
BEGIN.
OPEN INPUT WORKSHEET2.
READ WORKSHEET2
END-READ
DISPLAY NAME1.
CLOSE WORKSHEET2
STOP RUN.
stephen-gennards-macbook:fd spg$ cat WORKSHEETOUT2.dat
Hello World
stephen-gennards-macbook:fd spg$ ./fd
Hello World
Thanks, I'm really confused and my teacher is being no help. Changed the ASSIGN TO "WORKSHEET2.dat" and the program runs. Now I can get it to display the first line of the input file. The input file has several more lines that need to be read in and I'm not sure how to read the second line into another variable.

Also could someone tell me the easiest way to DISPLAY on the next line, in a PERFORM loop, without typing in LINE 2, or LINE 3?
 

·
Robot
Joined
·
865 Posts
Discussion Starter #4
Okay after some extensive googling I have got this far.

Code:
Code:
       IDENTIFICATION DIVISION.
       PROGRAM-ID. FILE-DISPLAY.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT WORKSHEET2 ASSIGN TO "WORKSHEET2.dat".
       DATA DIVISION.
       FILE SECTION.
       FD WORKSHEET2.
       01 INPUT-RECORD.
          02 IR-SYMBOL PIC X.
          02 IR-FIRSTNAME PIC X(13).
          02 IR-LASTNAME PIC X(7).
       WORKING-STORAGE SECTION.
       01 SWITCHES.
          02 SW-END-OF-FILE PIC X.
             88 END-OF-FILE        VALUE "Y".
       PROCEDURE DIVISION.
       BEGIN.
          OPEN INPUT WORKSHEET2.
          MOVE "N" TO SW-END-OF-FILE.
          READ WORKSHEET2
          PERFORM
            UNTIL END-OF-FILE.
          DISPLAY "***********************".
          DISPLAY IR-SYMBOL IR-FIRSTNAME IR-LASTNAME SPACE IR-SYMBOL.
          DISPLAY "***********************".
          READ WORKSHEET2
          END PERFORM.
          CLOSE WORKSHEET2.
       STOP RUN.
The file (WORKSHEET2.dat) has 4 people's names in it. This is the way it's formatted:

Code:
Code:
$Jessica      Alba
&Kim          Kardashian
$Reese        Witherspoon
&Jennifer     Aniston
I think I'm almost there. I just need help getting the program to loop through the whole file instead of just reading the first line. I can get it to read the first line if I take out the UNTIL END-OF-FILE command but of course it just goes straight to END PERFORM and doesn't continue to the next line. If anyone knows anything that could help please share.
 

·
Registered
Joined
·
1 Posts
Hi,

The problem with the code fragment you've posted is that you have statement terminators at the end of each line between the PERFORM verb and the END PERFORM.
PERFORM treats only the next statement as the loop body.
You need something more like

Code:

Code:
          PERFORM
            UNTIL END-OF-FILE
          DISPLAY "***********************"
          DISPLAY IR-SYMBOL IR-FIRSTNAME IR-LASTNAME SPACE IR-SYMBOL
          DISPLAY "***********************"
          READ WORKSHEET2
          END PERFORM.
 
1 - 5 of 5 Posts
Top