Problem compiling a Cobol program reading from diskfile - cobol

I am trying to learn Cobol using Murachs Mainframe Cobol book and GNUCobol 3.1 compiler. Currently I am stuck at Chapter 3, where I am suppose to read from a disk file and generate a report.
I have placed the .dat file in same folder as where I compile the program (and also in a /bin folder in same directory).
When I try to compile the program I receive the following error:
rose#eden~/cobol/Programs/Chapter 03 $ cobc -x RPT1000.CBL
RPT1000.CBL:11: error: redefinition of 'CUSTMAST'
RPT1000.CBL:11: error: 'CUSTMAST' previously defined here
RPT1000.CBL:12: error: redefinition of 'SALESRPT'
RPT1000.CBL:12: error: 'SALESRPT' previously defined here
Relevant Program code:
IDENTIFICATION DIVISION.
PROGRAM-ID. RPT1000.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CUSTMAST ASSIGN TO CUSTMAST.
SELECT SALESRPT ASSIGN TO SALESRPT.
DATA DIVISION.
FILE SECTION.
FD CUSTMAST.
01 CUSTOMER-MASTER-RECORD.
05 CM-BRANCH-NUMBER PIC 9(2).
05 CM-SALESREP-NUMBER PIC 9(2).
05 CM-CUSTOMER-NUMBER PIC 9(5).
05 CM-CUSTOMER-NAME PIC X(20).
05 CM-SALES-THIS-YTD PIC S9(5)V9(2).
05 CM-SALES-LAST-YTD PIC S9(5)V9(2).
FD SALESRPT.
01 PRINT-AREA PIC X(132).

SELECT CUSTMAST ASSIGN TO "bin/CUSTMAST.dat".
SELECT SALESRPT ASSIGN TO "bin/SALESRPT.dat".
Solved the problem ...

Related

OpenCOBOL can't find my external module to run the program

I'm trying to create a COBOL program using OpenCobol that has an external module when it does a calculation and then brings the result back into the main program using CALL. But everytime I try to run the program it says it can't find the module. I have already changed my module to Program Type "Module" from executable and I have added the module path as a Library path but nothing is working so far
Here is my main program code:
PROGRAM-ID. Project2 AS "Project2".
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
*****The student input file
SELECT STUDENT-FILE-IN
ASSIGN TO "C:\STUFILE.TXT"
ORGANIZATION IS LINE SEQUENTIAL.
*****The program input file
SELECT PROGRAM-FILE-IN
ASSIGN TO "C:\PROGRAM.TXT"
ORGANIZATION IS LINE SEQUENTIAL.
*****The student report output file
SELECT STUDENT-REPORT-FILE-OUT
ASSIGN TO "C:\REPORT.TXT"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
*Student input file
FD STUDENT-FILE-IN.
01 STUDENT-RECORD.
05 STUDENT-NUMBER PIC 9(6).
05 TUITION-OWED-IN PIC 9(4)V99.
05 STUDENT-NAME-IN PIC X(40).
05 PROGRAM-OF-STUDY PIC X(5).
05 COURSE-CODE-1 PIC X(7).
05 COURSE-AVERAGE-1 PIC 9(3).
05 COURSE-CODE-2 PIC X(7).
05 COURSE-AVERAGE-2 PIC 9(3).
05 COURSE-CODE-3 PIC X(7).
05 COURSE-AVERAGE-3 PIC 9(3).
05 COURSE-CODE-4 PIC X(7).
05 COURSE-AVERAGE-4 PIC 9(3).
05 COURSE-CODE-5 PIC X(7).
05 COURSE-AVERAGE-5 PIC 9(3).
*Program input file
FD PROGRAM-FILE-IN.
01 PROGRAM-RECORD-IN.
05 PROGRAM-CODE-IN PIC X(5).
05 PROGRAM-NAME-IN PIC X(20).
*Student report output file
FD STUDENT-REPORT-FILE-OUT.
01 STUDENT-REPORT-RECORD-OUT PIC x(90).
WORKING-STORAGE SECTION.
*Table to hold program records
01 PROGRAM-RECORD.
05 PROGRAM-TABLE.
10 PROGRAM-CODE PIC X(5) OCCURS 20 TIMES.
10 PROGRAM-NAME PIC X(20) OCCURS 20 TIMES.
*The student report record
01 STUDENT-REPORT-RECORD.
05 STUDENT-NAME PIC X(40).
05 FILLER PIC X(2) VALUE SPACES.
05 PROGRAM-NAME-OUT PIC X(20).
05 FILLER PIC X(4) VALUE SPACES.
05 TUITION-OWED PIC Z,ZZ9.99.
*A line to make the output look good
01 HEADER-LINE.
05 FILLER PIC X(90) VALUE ALL "-".
*The column header
01 COLUMN-HEADER.
05 NAME-COLUMN PIC X(42) VALUE "NAME".
05 AVG-COLUMN PIC X(7) VALUE "AVE".
05 PROG-COLUMN PIC X(24) VALUE "PROGRAM NAME".
05 OWED-COLUMN PIC X(12) VALUE "TUITION OWED".
01 CONTROL-FIELDS.
05 STUFILE-EOF-FLAG PIC A(3).
05 PROG-EOF-FLAG PIC A(3).
05 PROG-SUB PIC 9(2).
05 FOUND-PROG-FLAG PIC A(3).
01 COUNTERS.
05 STUDENT-RECORDS-READ-CTR PIC 9(3).
05 STUDENT-REPORTS-WRITTEN-CTR PIC 9(3).
01 WW-SEND-AREA.
05 STUDENT-AVERAGE PIC 9(3).
01 WW-SUB-PROG PIC X(75)
VALUE 'C:\Users\google\Program3Call'.
PROCEDURE DIVISION.
PERFORM 200-INITIALIZATION-RTN.
*Read and process all student records
PERFORM 200-PROCESS-STUDENT-RECORD-RTN
UNTIL STUFILE-EOF-FLAG = "YES".
PERFORM 200-FINISH-RTN.
STOP RUN.
200-INITIALIZATION-RTN.
PERFORM 300-OPEN-FILES-RTN.
*****Load all program records into the table
PERFORM 300-LOAD-PROGRAM-TABLE-RTN
VARYING PROG-SUB FROM 1 BY 1
UNTIL PROG-SUB > 20 OR PROG-EOF-FLAG = "YES".
PERFORM 300-INITIALIZE-REPORT-FILE-RTN.
PERFORM 300-INITIALIZE-COUNTERS-RTN.
200-PROCESS-STUDENT-RECORD-RTN.
PERFORM 300-READ-STUDENT-RECORD-RTN.
IF STUFILE-EOF-FLAG NOT EQUALS "YES"
*********Get the program name
PERFORM 300-GET-PROGRAM-NAME-RTN
VARYING PROG-SUB FROM 1 BY 1 UNTIL
FOUND-PROG-FLAG = "YES" OR PROG-SUB > 20
PERFORM 300-GET-STUDENT-AVERAGE
PERFORM 300-PRINT-STUDENT-REPORT-RTN
END-IF.
200-FINISH-RTN.
PERFORM 300-PRINT-COUNTERS-RTN.
PERFORM 300-CLOSE-FILES-RTN.
300-OPEN-FILES-RTN.
OPEN INPUT STUDENT-FILE-IN.
OPEN INPUT PROGRAM-FILE-IN.
OPEN OUTPUT STUDENT-REPORT-FILE-OUT.
300-LOAD-PROGRAM-TABLE-RTN.
READ PROGRAM-FILE-IN
AT END MOVE "YES" TO PROG-EOF-FLAG
NOT AT END MOVE PROGRAM-CODE-IN TO PROGRAM-CODE (PROG-SUB)
MOVE PROGRAM-NAME-IN TO PROGRAM-NAME (PROG-SUB).
300-INITIALIZE-REPORT-FILE-RTN.
MOVE COLUMN-HEADER TO STUDENT-REPORT-RECORD-OUT.
WRITE STUDENT-REPORT-RECORD-OUT.
MOVE HEADER-LINE TO STUDENT-REPORT-RECORD-OUT.
WRITE STUDENT-REPORT-RECORD-OUT.
300-INITIALIZE-COUNTERS-RTN.
MOVE 0 TO STUDENT-RECORDS-READ-CTR.
MOVE 0 TO STUDENT-REPORTS-WRITTEN-CTR.
300-READ-STUDENT-RECORD-RTN.
READ STUDENT-FILE-IN
AT END MOVE "YES" TO STUFILE-EOF-FLAG
NOT AT END ADD 1 TO STUDENT-RECORDS-READ-CTR.
300-GET-PROGRAM-NAME-RTN.
IF PROGRAM-CODE (PROG-SUB) = PROGRAM-OF-STUDY
MOVE PROGRAM-NAME (PROG-SUB) TO PROGRAM-NAME-OUT
MOVE "YES" TO FOUND-PROG-FLAG
END-IF.
300-GET-STUDENT-AVERAGE.
CALL WW-SUB-PROG USING WW-SEND-AREA.
300-PRINT-STUDENT-REPORT-RTN.
MOVE "NO" TO FOUND-PROG-FLAG.
MOVE STUDENT-NAME-IN TO STUDENT-NAME.
MOVE TUITION-OWED-IN TO TUITION-OWED.
MOVE STUDENT-REPORT-RECORD TO STUDENT-REPORT-RECORD-OUT.
WRITE STUDENT-REPORT-RECORD-OUT.
ADD 1 TO STUDENT-REPORTS-WRITTEN-CTR.
300-PRINT-COUNTERS-RTN.
DISPLAY "Student records read: ", STUDENT-RECORDS-READ-CTR.
DISPLAY "Student reports written: ",
STUDENT-REPORTS-WRITTEN-CTR.
300-CLOSE-FILES-RTN.
CLOSE STUDENT-FILE-IN, PROGRAM-FILE-IN
STUDENT-REPORT-FILE-OUT.
END PROGRAM Project2.
Here is my Call Program:
******************************************************************
* Author: Desiree Hanuman
* Date: August 14th 2021
* Purpose:Call Section code for project 3
* Tectonics: cobc
******************************************************************
IDENTIFICATION DIVISION.
PROGRAM-ID. Program3Call.
DATA DIVISION.
LINKAGE SECTION.
01 LS-STUDENT-AVERAGE-AREA.
05 COURSE-AVERAGE-1 PIC 9(3).
05 COURSE-AVERAGE-2 PIC 9(3).
05 COURSE-AVERAGE-3 PIC 9(3).
05 COURSE-AVERAGE-4 PIC 9(3).
05 COURSE-AVERAGE-5 PIC 9(3).
05 STUDENT-AVERAGE PIC 9(3).
PROCEDURE DIVISION.
MAIN-PROCEDURE.
100-STUDENT-AVERAGE.
PERFORM 300-CALCULATE-AVERAGE-RTN.
300-CALCULATE-AVERAGE-RTN.
COMPUTE STUDENT-AVERAGE ROUNDED = (COURSE-AVERAGE-1 +
COURSE-AVERAGE-2 + COURSE-AVERAGE-3 + COURSE-AVERAGE-4 +
COURSE-AVERAGE-5) / 5.
GOBACK.
The normal way to CALL in most COBOL implementations is to just specify the module name - without a path or extension.
Then you either do "static linking" (as in Scott's answer below for OpenCOBOL/GnuCOBOL just specify all sources on the command line and use either -x [fat executable] or -b [fat module, then callable for example with cobcrun], commonly with -static); or do "dynamic linking" (separate compilation of all modules) and specify the module path in a configuration file or by environment variable. For OpenCOBOL/GnuCOBOL this is COB_LIBRARY_PATH.
I am not a user of OpenCobol, but, according to their manual in section 7, there are many ways to accomplish what you want.
But the simplest would be to statically compile and link the main and sub programs in one command line:
cobc -x Project2.cbl Program3Call.cbl
This example work´s for me.
Please keep in mind that the name of the dll file routine module must be same as the routine name.
It is not necessary reconfigure OpenCobol path library.
I use OpenCobol 4.7.6.
The Code:
Main program (file build "MAINPROG.exe")
IDENTIFICATION DIVISION.
PROGRAM-ID. "MAINPROG".
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 X PIC X(1).
01 INPUTS.
05 PRM-LENGTH PIC S9(4) COMP.
05 INPUT1 PIC 9(2).
05 INPUT2 PIC 9(2).
PROCEDURE DIVISION.
MAIN-PARA.
MOVE 12 TO INPUT1.
MOVE 13 TO INPUT2.
CALL 'MFPROG1' USING PRM-LENGTH, INPUT1, INPUT2.
ACCEPT X.
STOP RUN.
END PROGRAM "MAINPROG".
Rutine program (file build module "MFPROG1.dll")
IDENTIFICATION DIVISION.
PROGRAM-ID. "MFPROG1".
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 OUTPUT-DATA PIC 9(4).
LINKAGE SECTION.
01 PARM-BUFFER.
05 PARM-LENGTH PIC S9(4) COMP.
05 PARM-INPUT1 PIC 9(2).
05 PARM-INPUT2 PIC 9(2).
PROCEDURE DIVISION USING PARM-BUFFER.
MAIN-PARA.
COMPUTE OUTPUT-DATA = PARM-INPUT1 + PARM-INPUT2.
DISPLAY 'PARM-INPUT1: ' PARM-INPUT1.
DISPLAY 'PARM-INPUT2: ' PARM-INPUT2.
DISPLAY 'SUM VALUE : ' OUTPUT-DATA.
GOBACK.
END PROGRAM "MFPROG1".

COBOL CAN'T WRITE RECORDS IN MY NOTEPAD FILES

I just want to know why i can't see my records on my Notepad files, is there something wrong in my code? I created my Notepad files into this path: "C:\Users\jamal.eclipse\MicroFocus\Student-Record.txt". When I run the code, it asks me the student number, the student tuition owed and the student name but can't see it in my Notepad files. Here's my code:
program-id. Project as "Project".
environment division.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT STUDENT-FILE-IN
ASSIGN TO
"C:\Users\jamal\.eclipse\MicroFocus\Student-Record.txt"
ORGANIZATION IS LINE SEQUENTIAL.
configuration section.
data division.
FILE Section.
FD STUDENT-FILE-IN.
01 STUDENT-RECORD-OUT PIC X(52).
working-storage section.
01 STUDENT-RECORD-IN.
05 STUDENT-NUMBER PIC 9(6).
05 TUITION-OWED PIC 9(6).
05 STUDENT-NAME PIC X(40).
01 PROMPT-MESSAGES-RESPONSES.
05 RECORD-Y-N PIC X(41) VALUE
"Do you have record ? Y or N: ".
05 RESPONSE PIC X(1).
05 STUDENT-NUMBER-OUT PIC X(24) VALUE
"Enter the Student Number: ".
05 TUITION-OWED-OUT PIC X(22) VALUE
"Enter the tuition owed: ".
05 STUDENT-NAME-OUT PIC X(22) VALUE
"Enter your Student Name: ".
procedure division.
CREATE-STUDENT-RECORD-FILE.
PERFORM STUDENT-RECORD-FILE.
PERFORM CREATE-STUDENT-RECORD
UNTIL RESPONSE="N" or "n".
PERFORM T-CREATE-STUDENT-RECORD-FILE.
STOP RUN.
STUDENT-RECORD-FILE.
PERFORM OPEN-RECORD.
PERFORM PROMPT-USER.
CREATE-STUDENT-RECORD.
PERFORM ENTER-STUDENT-DATA.
PERFORM WRITE-STUDENT-RECORD.
PERFORM PROMPT-USER.
T-CREATE-STUDENT-RECORD-FILE.
PERFORM CLS-CREATE-STUDENT-RECORD.
OPEN-RECORD.
OPEN OUTPUT STUDENT-FILE-IN.
PROMPT-USER.
DISPLAY RECORD-Y-N.
ACCEPT RESPONSE.
ENTER-STUDENT-DATA.
DISPLAY STUDENT-NUMBER-OUT.
ACCEPT STUDENT-NUMBER.
DISPLAY TUITION-OWED-OUT.
ACCEPT TUITION-OWED.
DISPLAY STUDENT-NAME-OUT
ACCEPT STUDENT-NAME.
WRITE-STUDENT-RECORD.
MOVE STUDENT-RECORD-IN TO STUDENT-RECORD-OUT.
WRITE STUDENT-RECORD-OUT.
CLS-CREATE-STUDENT-RECORD.
CLOSE STUDENT-FILE-IN.
end program Project.
I'm a beginner to Cobol, thank you in advance for the help.
Here is your program, i made some changes , i hope that will help you
IDENTIFICATION DIVISION.
PROGRAM-ID. project.
environment division.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT STUDENT-FILE-IN
ASSIGN TO "C:\Users\jamal\.eclipse\MicroFocus\Student-Record.txt"
ORGANIZATION IS LINE SEQUENTIAL.
configuration section.
data division.
FILE Section.
FD STUDENT-FILE-IN.
01 STUDENT-RECORD-OUT PIC X(52).
working-storage section.
01 STUDENT-RECORD-IN.
05 STUDENT-NUMBER PIC 9(6).
05 TUITION-OWED PIC 9(6).
05 STUDENT-NAME PIC X(40).
01 PROMPT-MESSAGES-RESPONSES.
05 RECORD-Y-N PIC X(41) VALUE
"Do you have record ? Y or N: ".
05 RESPONSE PIC X(1).
05 STUDENT-NUMBER-OUT PIC X(20) VALUE "Enter Student Number".
05 TUITION-OWED-OUT PIC X(20) VALUE "Enter tuition owed:".
05 STUDENT-NAME-OUT PIC X(20) VALUE "Enter Student Name:".
procedure division.
OPEN OUTPUT STUDENT-FILE-IN.
PERFORM PROMPT-USER.
PERFORM CREATE-STUDENT-RECORD
UNTIL RESPONSE="N" or "n".
PERFORM T-CREATE-STUDENT-RECORD-FILE.
CREATE-STUDENT-RECORD.
PERFORM ENTER-STUDENT-DATA.
PERFORM WRITE-STUDENT-RECORD.
PERFORM PROMPT-USER.
T-CREATE-STUDENT-RECORD-FILE.
PERFORM CLS-CREATE-STUDENT-RECORD.
PROMPT-USER.
DISPLAY RECORD-Y-N.
ACCEPT RESPONSE.
ENTER-STUDENT-DATA.
DISPLAY STUDENT-NUMBER-OUT.
ACCEPT STUDENT-NUMBER.
DISPLAY TUITION-OWED-OUT.
ACCEPT TUITION-OWED.
DISPLAY STUDENT-NAME-OUT
ACCEPT STUDENT-NAME.
WRITE-STUDENT-RECORD.
MOVE STUDENT-RECORD-IN TO STUDENT-RECORD-OUT.
WRITE STUDENT-RECORD-OUT.
CLS-CREATE-STUDENT-RECORD.
CLOSE STUDENT-FILE-IN
STOP RUN.

Is it possible to dynamically specify the file name at runtime in a COBOL program?

Is it possible to dynamically specify the file name at runtime in a COBOL program?
For example I would like to not have to define the name of a file I would like to open when I write the File-Control section, I would like to read the name of the file I want to open from a different file containing names and I would also like to define(FD) the file I want to open when I pass the name to File-COntrol.
The platform is Unix with Micro Focus Visual Cobol compiler.
I really love it when people ask for help posting a snippet of code that doesn't have a prayer of compiling.
IDENTIFICATION DIVISION.
PROGRAM-ID. STACK-OVERFLOW-1.
AUTHOR. Roland Hughes.
DATE-WRITTEN. TODAY.
DATE-COMPILED. TODAY.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT OPTIONAL OUT_FILE
ASSIGN TO DISK
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS IN-STAT.
DATA DIVISION.
FILE SECTION.
FD OUT_FILE
IS GLOBAL
VALUE OF FILE-ID IS OUT_FILE_NAME
LABEL RECORDS ARE STANDARD.
01 DTL-LINE.
03 FIELDA PIC X(14).
03 FIELDB PIC X(10).
03 FIELDC PIC X(32).
03 FIELDD PIC X(03).
03 FIELDE PIC X(02).
WORKING-STORAGE SECTION.
01 STATUS-VARIABLES.
05 IN-STAT PIC X(2).
01 STUFF.
05 OUT_FILE_NAME PIC X(80).
PROCEDURE DIVISION.
MOVE "FILE1.DAT" to OUT_FILE_NAME.
OPEN OUTPUT OUT_FILE.
MOVE SPACES TO DTL-LINE.
MOVE 'HELLO!' TO FIELDA.
WRITE DTL-LINE.
CLOSE OUT_FILE
MOVE "FILE2.DAT" to OUT_FILE_NAME.
OPEN OUTPUT OUT_FILE.
MOVE SPACES TO DTL-LINE.
MOVE 'HI!' TO FIELDB.
WRITE DTL-LINE.
CLOSE OUT_FILE.
MOVE "FILE3.DAT" to OUT_FILE_NAME.
OPEN OUTPUT OUT_FILE.
MOVE SPACES TO DTL-LINE.
MOVE 'FRED' TO FIELDC.
WRITE DTL-LINE.
CLOSE OUT_FILE.
STOP RUN.
I tested this on KDE Neon with all of the latest updates installed. I installed the open-cobol package from the repositories.
cobc -x -free STACK-OVERFLOW-1.COB
./STACK-OVERFLOW-1
roland#roland-HP-EliteDesk-800-G2-SFF:~/COBOL$ ls *.DAT
FILE1.DAT FILE2.DAT FILE3.DAT
roland#roland-HP-EliteDesk-800-G2-SFF:~/COBOL$ cat *.DAT
HELLO! HI! FRED roland#roland-HP-EliteDesk-800-G2-SFF:~/COBOL$
A more complete example:
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN_FILE ASSIGN TO IN_FILE
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS FSTAT.
FILE SECTION.
FD IN_FILE
VALUE OF ID IS IN_FILE_NAME.
01 IN_POST.
03 FIELDA PIC X(14).
03 FIELDB PIC X(10).
03 FIELDC PIC X(32).
03 FIELDD PIC X(03).
03 FIELDE PIC X(02).
WORKING-STORAGE SECTION.
01 IN_FILE_NAME PIC X(22).
01 FSTAT PIC XX.
PROCEDURE DIVISION.
MOVE "FILE1.DAT" to IN_FILE_NAME
OPEN INPUT IN_FILE
CLOSE IN_FILE
MOVE "FILE2.DAT" to IN_FILE_NAME
OPEN INPUT IN_FILE
CLOSE IN_FILE
MOVE "FILE3.DAT" to IN_FILE_NAME
OPEN INPUT IN_FILE
CLOSE IN_FILE
STOP RUN.

saving output in a file using COBOL language

I need help writing a small program in COBOL.
I wrote this piece of code:
IDENTIFICATION DIVISION.
PROGRAM-ID. CallC.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INITBUF PIC X(10).
01 SHOWBUF PIC X(10).
01 BUF USAGE IS POINTER.
01 NUM PIC 9(10).
PROCEDURE DIVISION.
MOVE 10 to NUM.
CALL "getBuffer" USING BY VALUE NUM RETURNING BUF.
CALL "initBuffer" USING BY VALUE BUF RETURNING INITBUF.
CALL "showBuffer" USING BY VALUE BUF RETURNING SHOWBUF.
DISPLAY SHOWBUF.
STOP RUN.
How do I go about writing the DISPLAY result to a file?
Bill. Declare the file as in the example below. Move whatever you want to output into the output variable then Write outputVar
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT PARM-OUT ASSIGN TO PARMOUT.
*************************************************
DATA DIVISION.
FILE SECTION.
FD PARM-OUT
RECORDING MODE F.
01 PARMIN-REC.
05 PSID-IN PIC 9(09).
05 PCID-IN PIC 9(08).
05 IN-PCIDSEQ PIC 9(03).

Reading the last record of a file

I have a COBOL program that requires a transaction number every run of the program. I am planning to get the last transaction number from the file and add 1 to it. The problem is I don't know how to get the last recorded value.
IDENTIFICATION DIVISION.
PROGRAM-ID. INVENTORY-SYS.
AUTHOR. LINSEY.
DATE-WRITTEN. 2/22/2015.
DATE-COMPILED. 2/22/2015.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT MASTER-FILE ASSIGN TO "inventory-file.txt"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD MASTER-FILE.
01 IN-RECORDS.
02 IN-CODE PIC 9(7).
02 IN-NAME PIC X(30).
02 IN-PRICE PIC 9(3).
02 IN-STOCK PIC 9(4).
WORKING-STORAGE SECTION.
01 WS-EOF PIC A(1).
PROCEDURE DIVISION.
100-READ-FILE.
OPEN I-O MASTER-FILE.
PERFORM UNTIL WS-EOF = "Y"
READ MASTER-FILE
AT END
MOVE 'Y' TO WS-EOF
NOT AT END
DISPLAY IN-RECORDS
END-READ
END-PERFORM
CLOSE MASTER-FILE.
STOP RUN.
This is the sample program. The problem is it retrieves all the records from the file I only the last record from "inventory-file.txt"
Well, you are insistent, so:
IDENTIFICATION DIVISION.
PROGRAM-ID. INVENTORY-SYS.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT MASTER-FILE ASSIGN TO "inventory-file.txt"
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS w-if-in-file-status.
DATA DIVISION.
FILE SECTION.
FD MASTER-FILE.
01 IN-RECORDS.
02 IN-CODE PIC 9(7).
02 IN-NAME PIC X(30).
02 IN-PRICE PIC 9(3).
02 IN-STOCK PIC 9(4).
WORKING-STORAGE SECTION.
01 w-if-in-file-status PIC XX.
88 master-file-status-good VALUE ZERO "10".
88 end-of-master-file VALUE "10".
01 w-save-code PIC 9(7).
PROCEDURE DIVISION.
OPEN INPUT MASTER-FILE
[code to check FILE STATUS field]
PERFORM UNTIL end-of-master-file
READ MASTER-FILE
[code to check FILE STATUS field]
MOVE IN-CODE to w-save-code
END-PERFORM
DISPLAY ">" w-save-code "<"
CLOSE MASTER-FILE
[code to check FILE STATUS field]
GOBACK
.
I don't know why your IN-CODE field is numeric. Are you going to do calculations with it?
You keep a Control File. That has a date (to match to the Business Date file) a logical file-name, an environment and the last transaction number.
You maintain that file, checking everything as you do so.
I'm not sure of OpenCobol supports it, but you might try using OPEN/REVERSED. It is an old school way of reading a tape backwards, or these days, a virtual tape. I've no idea if it is implemented, but it is an easy way to read a sequential file backwards.

Resources