I wanted to write a cobol program to read a file EMP-GRADE with 4 fields(EMPID,NAME,STREAM,GRADE) and display the details of employees who have scored a grade 'A' in the output. I used on-line Cobol compiler.
IDENTIFICATION DIVISION.
PROGRAM-ID. SAMPLE1.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT EMPFILE ASSIGN TO 'input.txt'
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS WS-FILE-STATUS.
SELECT EMPA ASSIGN TO 'util.cobc'
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD EMPFILE.
01 NEWFILE.
05 FS-EMPID PIC 9(6).
05 FILLER PIC X(50).
05 FS-NAME PIC A(10).
05 FILLER PIC X(50).
05 FS-STREAM PIC A(10).
05 FILLER PIC X(50).
05 FS-GRADE PIC A(1).
05 FILLER PIC X(50).
FD EMPA.
01 OUTFILE.
05 FS-EMPID-OUT PIC 9(6).
05 FILLER PIC X(50).
05 FS-NAME-OUT PIC A(10).
05 FILLER PIC X(50).
05 FS-STREAM-OUT PIC A(10).
05 FILLER PIC X(50).
05 FS-GRADE-OUT PIC A(1).
05 FILLER PIC X(50).
WORKING-STORAGE SECTION.
01 WS-EOF PIC A(1) VALUE "N".
01 WS-FILE-STATUS PIC X(2).
PROCEDURE DIVISION.
MAIN-PARA.
OPEN INPUT EMPFILE
OPEN OUTPUT EMPA
PERFORM READ-PARA THRU READ-PARA-EXIT
Perform Until WS-EOF = "Y"
IF FS-GRADE='A'
MOVE NEWFILE TO OUTFILE
WRITE OUTFILE
END-WRITE
DISPLAY OUTFILE
ELSE
DISPLAY 'NO A GRADE STUDENT IN THE LIST'
END-IF
PERFORM READ-PARA THRU READ-PARA-EXIT
END-PERFORM
CLOSE EMPFILE.
CLOSE EMPA.
STOP RUN.
MAIN-PARA-EXIT.
EXIT.
READ-PARA.
READ EMPFILE
AT END MOVE "Y" TO WS-EOF
NOT AT END DISPLAY NEWFILE
END-READ.
READ-PARA-EXIT.
EXIT.
The above is my Updated code and then I gave input in the 'input.txt' and I got output as follows
11111 AISHU JAVA B
22222 RANJU MAINF A
NO A GRADE STUDENT IN THE LIST'
Try this code:
PROCEDURE DIVISION.
MAIN-PARA.
OPEN INPUT EMPFILE
OPEN OUTPUT EMPA
PERFORM READ-PARA THRU READ-PARA-EXIT UNTIL WS-EOF = ‘Y’
CLOSE EMPFILE.
CLOSE EMPA.
STOP RUN.
MAIN-PARA-EXIT.
EXIT.
READ-PARA.
READ EMPFILE
AT END
MOVE "Y" TO WS-EOF
IF WS-F='M' THEN
DISPLAY 'NO A GRADE STUDENT IN THE LIST'
END-IF
NOT AT END DISPLAY NEWFILE
IF FS-GRADE='A' THEN
MOVE ‘M’ TO WS-F
MOVE NEWFILE TO OUTFILE
WRITE OUTFILE
END-WRITE
DISPLAY OUTFILE
ELSE
CONTINUE
END-IF
END-READ.
READ-PARA-EXIT.
EXIT.
You are reading your entire input file with the
PERFORM READ-PARA THRU READ-PARA-EXIT UNTIL WS-EOF="Y"
statement. You must instead read a record, process it, read the next record, process it, and so forth.
OPEN INPUT EMPFILE
OPEN OUTPUT EMPA
PERFORM READ-PARA THRU READ-PARA-EXIT
Perform Until WS-EOF = "Y"
IF FS-GRADE='A'
MOVE NEWFILE TO OUTFILE
WRITE OUTFILE
DISPLAY OUTFILE
END-IF
PERFORM READ-PARA THRU READ-PARA-EXIT
End-Perform
CLOSE EMPFILE
CLOSE EMPA
STOP RUN.
There are probably any number of other improvements to be made based on the actual requirements for the exercise, but I think this solves your immediate problem.
Note the two separate PERFORMs of READ-PARA. The first is sometimes called a priming read, which is a helpful technique to learn.
Related
I am writing COBOL pgm to sum 2 numbers, passing these 2 numbers from JCL in an input file and storing their sum in output file. But I am getting SOC4 (at MOVE). Below is my code. Please advise
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILEIN ASSIGN TO INPFILE
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS WS-ST1.
SELECT FILEOUT ASSIGN TO OUTFILE
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS WS-ST2.
DATA DIVISION.
FILE SECTION.
FD FILEIN.
01 FIN-REC.
05 FIN-NUM1 PIC 9(2).
05 FILLER PIC X(1).
05 FIN-NUM2 PIC 9(2).
05 FILLER PIC X(75).
FD FILEOUT.
01 FOUT-TOT PIC 9(2).
01 FILLER PIC X(78).
WORKING-STORAGE SECTION.
01 WS-REC.
05 WS-NUM1 PIC 9(2).
05 WS-NUM2 PIC 9(2).
01 WS-ST1 PIC X(2) VALUE SPACES.
01 WS-ST2 PIC X(2) VALUE SPACES.
01 WS-EOF PIC X(1) VALUE SPACE.
01 WS-SUM PIC 9(2).
PROCEDURE DIVISION.
MAIN-PARA.
PERFORM 100-INITIAL-PARA THRU 100-EXIT.
PERFORM 200-PROCESS-PARA THRU 200-EXIT
UNTIL WS-EOF='Y'.
PERFORM 300-COMPUTE-PARA THRU 300-EXIT.
PERFORM 400-WRITE-PARA THRU 400-EXIT.
STOP RUN.
100-INITIAL-PARA.
MOVE 'N' TO WS-EOF
OPEN INPUT FILEIN
IF WS-ST1 NOT = '00'
DISPLAY 'ERROR IN 100-INITIAL-PARA'
DISPLAY 'INPUT FILE OPEN STATUS IS' WS-ST1
DISPLAY 'OUTPUT FILE OPEN STATUS IS' WS-ST2
END-IF.
100-EXIT.
EXIT.
200-PROCESS-PARA.
PERFORM UNTIL WS-EOF='Y'
READ FILEIN INTO WS-REC
AT END MOVE 'Y' TO WS-EOF
NOT AT END DISPLAY WS-REC
END-READ
END-PERFORM
CLOSE FILEIN.
200-EXIT.
EXIT.
300-COMPUTE-PARA.
COMPUTE WS-SUM= WS-NUM1 + WS-NUM2.
300-EXIT.
EXIT.
400-WRITE-PARA.
OPEN OUTPUT FILEOUT.
MOVE WS-SUM TO FOUT-TOT.
*> SOC4 abend in MOVE above
WRITE FOUT-TOT
END-WRITE.
CLOSE FILEOUT.
400-EXIT.
EXIT.
Below is the run JCL
//A102153J JOB MSGCLASS=S,MSGLEVEL=(1,1),NOTIFY=&SYSUID,REGION=0M
//STEP001 EXEC PGM=EXER4
//STEPLIB DD DISP=SHR,DSN=ADESH.LOADLIB
//INPFILE DD DSN=ADESH.EXER4.INFILE,DISP=SHR
//OUTFILE DD DSN=ADESH.EXER4.OUTFILE,UNIT=SYSDA,
// DISP=(NEW,CATLG,DELETE),SPACE=(CYL,(1,1),RLSE),
// DCB=(RECFM=FB,LRECL=80,BLKSIZE=0)
//SYSOUT DD SYSOUT=*
//SYSPRINT DD SYSOUT=*
Problem could be the output declaration:
FD FILEOUT.
01 FOUT-TOT PIC 9(2).
01 FILLER PIC X(78).
Replace with
FD FILEOUT.
01 out-record.
03 FOUT-TOT PIC 9(2).
03 FILLER PIC X(78).
Also update the write to
Write out-record.
Reason for the problem is the 2 01 levels --> VB which is different to the FB definition in the JCL which will cause the open to fail
There are a couple of logic errors in you program.
Firstly, in
100-INITIAL-PARA.
MOVE 'N' TO WS-EOF
OPEN INPUT FILEIN
IF WS-ST1 NOT = '00'
DISPLAY 'ERROR IN 100-INITIAL-PARA'
DISPLAY 'INPUT FILE OPEN STATUS IS' WS-ST1
DISPLAY 'OUTPUT FILE OPEN STATUS IS' WS-ST2
END-IF.
You open the input file, only, but check (well display) the status of the output file, which is yet to be opened.
Secondly, in the main paragraph
MAIN-PARA.
...
PERFORM 200-PROCESS-PARA THRU 200-EXIT
UNTIL WS-EOF='Y'.
you code a repetitive loop to end at end of input. And the called section
200-PROCESS-PARA.
PERFORM UNTIL WS-EOF='Y'
READ FILEIN INTO WS-REC
AT END MOVE 'Y' TO WS-EOF
NOT AT END DISPLAY WS-REC
END-READ
END-PERFORM
CLOSE FILEIN.
200-EXIT.
EXIT.
is again a repetitive loop to end at end of input file. There is one loop to many here.
Thirdly, in the section to write the output, you open the output file but are missing to check the status thereafter.
400-WRITE-PARA.
OPEN OUTPUT FILEOUT.
MOVE WS-SUM TO FOUT-TOT.
WRITE FOUT-TOT
END-WRITE.
CLOSE FILEOUT.
400-EXIT.
EXIT.
Lastly, the sum of two two digit numbers may well become a three digit number. You sum fields are declared as 2 digit field, however.
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.
OK so I'm doing assignment but then I found that I was asked to add page numbers and change pages for each 4 records. Since it's an online course and I don't think there is anything about page numbers in lecture videos. So the main problems are
To add a heading that contains date and page number,
Print 4 records per page, which means page needs to be changed after printing 4 records.
I really have no idea how to do this.
Here is the code I have finished:
ENVIRONMENT DIVISION.
FILE-CONTROL. SELECT STOCK-IN ASSIGN TO 'F:/CS201S13/PROJECT2.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT STOCK-OUT ASSIGN TO 'F:/CS201S13/PROJECT2OUTPUT.TXT'
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD STOCK-IN.
01 STOCK-RECORD.
05 ST-TRANSACTION-INFORMATION.
10 ST-TRANSACTION-SHARES PIC 9(3).
10 ST-TRANSACTION-STOCK PIC X(14).
05 ST-PURCHASE-INFORMATION.
10 ST-PURCHASE-PRICE PIC 9(5)V99.
10 ST-PURCHASE-DATE.
15 ST-PURCHASE-YEAR PIC 99.
15 ST-PURCHASE-MONTH PIC 99.
15 ST-PURCHASE-DAY PIC 99.
05 ST-SALE-INFORMATION.
10 ST-SALE-PRICE PIC 9(5)V99.
10 ST-SALE-DATE.
15 ST-SALE-YEAR PIC 99.
15 ST-SALE-MONTH PIC 99.
15 ST-SALE-DAY PIC 99.
FD STOCK-OUT.
01 STOCK-RECORD-OUT.
05 ST-TRANSACTION-INFORMATION-OUT.
10 ST-TRANSACTION-SHARES-OUT PIC 9(3).
10 ST-TRANSACTION-STOCK-OUT PIC X(14).
05 TOTAL-PURCHASE PIC 9(8)V99.
05 PIC X(4).
05 TOTAL-SALE PIC 9(8)V99.
05 PIC X(4).
05 TOTAL-PROFIT PIC 9(8)V99.
05 PIC X(4).
05 ST-PURCHASE-DATE-OUT.
10 ST-PURCHASE-YEAR-OUT PIC 99.
10 PIC X VALUE '/'.
10 ST-PURCHASE-MONTH-OUT PIC 99.
10 PIC X VALUE '/'.
10 ST-PURCHASE-DAY-OUT PIC 99.
05 PIC X(4).
05 ST-SALE-DATE-OUT.
10 ST-SALE-YEAR-OUT PIC 99.
10 PIC X VALUE '/'.
10 ST-SALE-MONTH-OUT PIC 99.
10 PIC X VALUE '/'.
10 ST-SALE-DAY-OUT PIC 99.
05 PIC X(4).
05 RECORD-OUT PIC 9 VALUE 0.
05 PAGE-OUT PIC 9.
WORKING-STORAGE SECTION.
01 ARE-THERE-MORE-RECORDS PIC XXX VALUE 'YES'.
01 IS-THIS-PAGE-FULL PIC XXX VALUE 'NO '.
PROCEDURE DIVISION.
100-MAIN-PROCESS.
OPEN INPUT STOCK-IN
OUTPUT STOCK-OUT
MOVE ST-TRANSACTION-INFORMATION TO ST-TRANSACTION-INFORMATION-OUT
PERFORM UNTIL ARE-THERE-MORE-RECORDS = 'NO '
READ STOCK-IN
AT END
MOVE 'NO ' TO ARE-THERE-MORE-RECORDS
NOT AT END
PERFORM 200-PROCEDURE-RTN
ADD 1 TO RECORD-OUT
END-READ
END-PERFORM
CLOSE STOCK-IN
STOCK-OUT
STOP RUN.
200-PROCEDURE-RTN.
IF RECORD-OUT = 4
MOVE 'YES' TO IS-THIS-PAGE-FULL
MOVE 0 TO RECORD-OUT
MOVE 'NO ' TO IS-THIS-PAGE-FULL
ADD 1 TO PAGE-OUT
END-IF
MULTIPLY ST-PURCHASE-PRICE BY ST-TRANSACTION-SHARES GIVING TOTAL-PURCHASE
MULTIPLY ST-SALE-PRICE BY ST-TRANSACTION-SHARES GIVING TOTAL-SALE
SUBTRACT TOTAL-PURCHASE FROM TOTAL-SALE GIVING TOTAL-PROFIT
WRITE STOCK-RECORD-OUT.
You are both close, and far away.
"Close" because you need a little bit of code in between setting IS-THIS-PAGE-FULL to YES and NO.
"Far away" as you have quite a lot to do rather than just "patch up" what you have.
Is the program writing an output file (STOCK-OUT) and a report, or is STOCK-OUT the report? If it is a report, change the names so that it is clear that it is a report, not an output file.
Don't worry if this seems a lot. You should be learning how to Program in Cobol, as well as learning Cobol. Doesn't happen overnight.
In no particular order:
Include FILE-STATUS checking for all IO operations on all files, always. At the moment, if your input fails to open and the system does not fail the program (even if yours does, you are presumably learning Cobol to be able to work with any system, not just the one you have) then no records will be read, your "end of file test" will never be YES and you'll have a BFL (Big Fat Loop). With the FILE-STATUS checking, produce useful messages, including key/reference/record number as appropriate for failed READ or WRITE.
You may feel that this is a lot of work. However, put together some "template" files with all the stuff in, and then paste (or even COPY) those into your program each time.
You have VALUE clause in the FD. These will not do what you think.
You have single digit for your page count, which is unlikely to have general application.
Why use YES and NO as literals? Look at the SET verb, in relation to "condition names", use 88's for tests and "flags/switches".
You have "MOVE ST-TRANSACTION-INFORMATION" after the input is opened but before a record is read, and only have one reference to it in the program. This is not going to work.
For reading files, have a look at the "priming read" approach.
read input
loop until end-of-file (88 on file-status)
process data
read input
end-loop
This avoids the AT END/NOT AT END, allows processing of headers (if present) and "empty files" without clogging-up the main logic. The code "expands" with headers/trailers (including the correct number of them), sequence-checking of keys, etc, but you only need to code it once then "template" it.
According to your VALUE clauses in your FD, you expect RECORD-OUT to be zero, so the test for 4 will actually get you five on the first page, and four thereafter.
You always assume there will be a "profit" (a positive amount), which is not realistic, yet you don't allow a signed value for the "profit".
Now, for the report.
For your report FD, just make it a simple thing, length of your print line.
In WORKING-STORAGE, define data for the headings and titles that you need. Define data for a print line. Since you're in the WORKING-STORAGE, put VALUEs for everything which will not have data MOVEd to it in the PROCEDURE DIVISION.
When you have written four items (or when your program tells you this) and you have a fifth, write the headings and titles, remembering to update the page number.
I say "or when your program tells you this" because you can set your original value of "records written" to 4. Comment it, so that it is clear that it is what you want, and why you want it. The reason is, you don't have to then deal with "first time" headings and othe things. For first time, or on a "contol break" (I guess you'll get to those soon) set the " done on a page already" to the maximum for a page, and the headings will pop out when you want.
Format the print line. PERFORM a para to print it (which is where the "page full" test will be).
Note: You can use VALUEs for your "/"s in the dates, or you can use the "/" editing character in the PICture, like this:
05 an-input-date PIC X(8) (can be other definitions).
...
05 date-to-print PIC X(4)/XX/XX.
...
MOVE an-input-date TO date-to-print
I like to see that you are using "minimal full-stops/periods". You can go a little further.
MOVE an-input-date TO date-to-print
.
Then you get your final full-stop/period in a paragraph, without having it "attached" to any particular line of code, which makes "tossing code around" easier, as you don't have to think "do I need/not need that full-stop/period there".
You could also look through some of the Cobol questions here, and get a handle on some general tips and advice.
This may or may not help, if LINAGE is not supported you'll have to do some explicit counting.
*****************************************************************
* Example of LINAGE File Descriptor
* Author: Brian Tiffin
* Date: 10-July-2008
* Tectonics: $ cobc -x linage.cob
* $ ./linage <filename ["linage.cob"]>
* $ cat -n mini-report
*****************************************************************
IDENTIFICATION DIVISION.
PROGRAM-ID. linage-demo.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
select optional data-file assign to file-name
organization is line sequential
file status is data-file-status.
select mini-report assign to "mini-report".
DATA DIVISION.
FILE SECTION.
FD data-file.
01 data-record.
88 endofdata value high-values.
02 data-line pic x(80).
FD mini-report
linage is 16 lines
with footing at 15
lines at top 2
lines at bottom 2.
01 report-line pic x(80).
WORKING-STORAGE SECTION.
01 command-arguments pic x(1024).
01 file-name pic x(160).
01 data-file-status pic 99.
01 lc pic 99.
01 report-line-blank.
02 filler pic x(18) value all "*".
02 filler pic x(05) value spaces.
02 filler pic x(34)
VALUE "THIS PAGE INTENTIONALLY LEFT BLANK".
02 filler pic x(05) value spaces.
02 filler pic x(18) value all "*".
01 report-line-data.
02 body-tag pic 9(6).
02 line-3 pic x(74).
01 report-line-header.
02 filler pic x(6) VALUE "PAGE: ".
02 page-no pic 9999.
02 filler pic x(24).
02 filler pic x(5) VALUE " LC: ".
02 header-tag pic 9(6).
02 filler pic x(23).
02 filler pic x(6) VALUE "DATE: ".
02 page-date pic x(6).
01 page-count pic 9999.
PROCEDURE DIVISION.
accept command-arguments from command-line end-accept.
string
command-arguments delimited by space
into file-name
end-string.
if file-name equal spaces
move "linage.cob" to file-name
end-if.
open input data-file.
read data-file
at end
display
"File: " function trim(file-name)
" open error or empty"
end-display
go to early-exit
end-read.
open output mini-report.
write report-line
from report-line-blank
end-write.
move 1 to page-count.
accept page-date from date end-accept.
move page-count to page-no.
write report-line
from report-line-header
after advancing page
end-write.
perform readwrite-loop until endofdata.
display
"Normal termination, file name: "
function trim(file-name)
" ending status: "
data-file-status
end-display.
close mini-report.
* Goto considered harmful? Bah! :)
early-exit.
close data-file.
exit program.
stop run.
****************************************************************
readwrite-loop.
move data-record to report-line-data
move linage-counter to body-tag
write report-line from report-line-data
end-of-page
add 1 to page-count end-add
move page-count to page-no
move linage-counter to header-tag
write report-line from report-line-header
after advancing page
end-write
end-write
read data-file
at end set endofdata to true
end-read
.
*****************************************************************
* Commentary
* LINAGE is set at a 20 line logical page
* 16 body lines
* 2 top lines
* A footer line at 15 (inside the body count)
* 2 bottom lines
* Build with:
* $ cobc -x -Wall -Wtruncate linage.cob
* Evaluate with:
* $ ./linage
* This will read in linage.cob and produce a useless mini-report
* $ cat -n mini-report
*****************************************************************
END PROGRAM linage-demo.
How I can read a .dat file with struct like that: ( A = ALPHANUMERIC && N = NUMERIC )
0AAAAAAAANNNN (233 BLANK SPACES ) 999999 ( SEQUENTIAL NUMBER ONE BY ONE )
1NNNNNNNNNNNNAAAAAAAAAAAAAAAAAAA (194 BLANK SPACES) 999999 (SEQUENTIAL NUMBER ONE BY ONE)
2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA (194 BLANK SPACES) 999999 (SEQUENTIAL NUMBER ONE BY ONE)
1NNNNNNNNNNNNAAAAAAAAAAAAAAAAAAA (194 BLANK SPACES) 999999 (SEQUENTIAL NUMBER ONE BY ONE)
2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA (194 BLANK SPACES) 999999 (SEQUENTIAL NUMBER ONE BY ONE)
1NNNNNNNNNNNNAAAAAAAAAAAAAAAAAAA (194 BLANK SPACES) 999999 (SEQUENTIAL NUMBER ONE BY ONE)
2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA (194 BLANK SPACES) 999999 (SEQUENTIAL NUMBER ONE BY ONE)
1NNNNNNNNNNNNAAAAAAAAAAAAAAAAAAA (194 BLANK SPACES) 999999 (SEQUENTIAL NUMBER ONE BY ONE)
2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA (194 BLANK SPACES) 999999 (SEQUENTIAL NUMBER ONE BY ONE)
9 (245 BLANK SPACES) 999999 (SEQUENTIAL NUMBER ONE BY ONE)
So, I know, how I can make a program to read this in C/C++ or in C#, but, I try to make in Cobol, just for study....
But, I don't know what the command I need to use to open the file with this style ( I just know the:
ORGANIZATION IS LINE SEQUENTIAL.
I think, exist a another command to open with custon instructions... i don't know...
So, btw, how I can open the file and read the informations ??
( i just need to read the line 1 on time, and, I need to read the line 2 and 3 always paried ... 4 and 5 && 6 and 7 && 8 and 9 ... )
and, I whant to show that information with DISPLAY ( just for study )
Thanks :)
Something like this below your FD:
01 INPUT-RECORD.
05 IR-RECORD-TYPE PIC X.
88 INPUT-RECORD-IS-HEADER VALUE '0'.
88 INPUT-RECORD-IS-DATA1 VALUE '1'.
88 INPUT-RECORD-IS-DATA2 VALUE '2'.
88 INPUT-RECORD-IS-TRAILER VALUE '9'.
05 FILLER PIC X(whatever).
You may need a "trailing" byte for a record-delimiter, I don't know, and you'll have to sort out the lengths, as they seem to vary.
These in Working-Storage:
01 INPUT-RECORD-HEADER.
05 IRH-RECORD-TYPE PIC X.
05 IRH-ITEM1 PIC X(8).
05 IRH-ITEM2 PIC 9(4).
05 FILLER PIC X(233).
05 IRH-SEQUENCE PIC X(6)
01 INPUT-RECORD-DATA1.
05 IRD1-RECORD-TYPE PIC X.
05 IRD1-ITEM1 PIC 9(14).
05 IRD1-ITEM1 PIC X(19).
05 FILLER PIC X(194).
05 IRD1-SEQUENCE PIC X(6)
01 INPUT-RECORD-DATA2.
05 IRD2-RECORD-TYPE PIC X.
05 IRD2-ITEM1 PIC X(33).
05 FILLER PIC X(194).
05 IRD2-SEQUENCE PIC X(6)
01 INPUT-RECORD-TRAILER.
05 IRT-RECORD-TYPE PIC X.
05 FILLER PIC X(245).
05 IRT-SEQUENCE PIC X(6).
You have to read each record, one at a time. Identify it. Put it in the correct W-S definition. When you read a "2" you can process the "1" you have stored along with the "2".
My datanames aren't very good, as I don't know what your data is. Also I have not "formatted" the definitions, which will make them more readable when you do it.
For OpenCOBOL, here is a sample standard in/standard out filter program:
>>SOURCE FORMAT IS FIXED
*> ***************************************************************
*><* ===========
*><* filter
*><* ===========
*><* :Author: Brian Tiffin
*><* :Date: 20090207
*><* :Purpose: Standard IO filters
*><* :Tectonics: cobc -x filter.cob
*> ***************************************************************
identification division.
program-id. filter.
environment division.
configuration section.
input-output section.
file-control.
select standard-input assign to keyboard.
select standard-output assign to display.
data division.
file section.
fd standard-input.
01 stdin-record pic x(32768).
fd standard-output.
01 stdout-record pic x(32768).
working-storage section.
01 file-status pic x value space.
88 end-of-file value high-value
when set to false is low-value.
*> ***************************************************************
procedure division.
main section.
00-main.
perform 01-open
perform 01-read
perform
until end-of-file
perform 01-transform
perform 01-write
perform 01-read
end-perform
.
00-leave.
perform 01-close
.
goback.
*> end main
support section.
01-open.
open input standard-input
open output standard-output
.
01-read.
read standard-input
at end set end-of-file to true
end-read
.
*> All changes here
01-transform.
move stdin-record to stdout-record
.
*>
01-write.
write stdout-record end-write
.
01-close.
close standard-input
close standard-output
.
end program filter.
*><*
*><* Last Update: dd-Mmm-yyyy
and here is a demonstration of using LINAGE that just happens to read in a text file.
*****************************************************************
* Example of LINAGE File Descriptor
* Author: Brian Tiffin
* Date: 10-July-2008
* Tectonics: $ cocb -x linage-demo.cob
* $ ./linage-demo <filename ["linage-demo.cob"]>
* $ cat -n mini-report
*****************************************************************
IDENTIFICATION DIVISION.
PROGRAM-ID. linage-demo.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
select optional data-file assign to file-name
organization is line sequential
file status is data-file-status.
select mini-report assign to "mini-report".
DATA DIVISION.
FILE SECTION.
FD data-file.
01 data-record.
88 endofdata value high-values.
02 data-line pic x(80).
FD mini-report
linage is 16 lines
with footing at 15
lines at top 2
lines at bottom 2.
01 report-line pic x(80).
WORKING-STORAGE SECTION.
01 command-arguments pic x(1024).
01 file-name pic x(160).
01 data-file-status pic 99.
01 lc pic 99.
01 report-line-blank.
02 filler pic x(18) value all "*".
02 filler pic x(05) value spaces.
02 filler pic x(34)
VALUE "THIS PAGE INTENTIONALLY LEFT BLANK".
02 filler pic x(05) value spaces.
02 filler pic x(18) value all "*".
01 report-line-data.
02 body-tag pic 9(6).
02 line-3 pic x(74).
01 report-line-header.
02 filler pic x(6) VALUE "PAGE: ".
02 page-no pic 9999.
02 filler pic x(24).
02 filler pic x(5) VALUE " LC: ".
02 header-tag pic 9(6).
02 filler pic x(23).
02 filler pic x(6) VALUE "DATE: ".
02 page-date pic x(6).
01 page-count pic 9999.
PROCEDURE DIVISION.
accept command-arguments from command-line end-accept.
string
command-arguments delimited by space
into file-name
end-string.
if file-name equal spaces
move "linage-demo.cob" to file-name
end-if.
open input data-file.
read data-file
at end
display
"File: " function trim(file-name) " open error"
end-display
go to early-exit
end-read.
open output mini-report.
write report-line
from report-line-blank
end-write.
move 1 to page-count.
accept page-date from date end-accept.
move page-count to page-no.
write report-line
from report-line-header
after advancing page
end-write.
perform readwrite-loop until endofdata.
display
"Normal termination, file name: "
function trim(file-name)
" ending status: "
data-file-status
end-display.
close mini-report.
* Goto considered harmful? Bah! :)
early-exit.
close data-file.
exit program.
stop run.
****************************************************************
readwrite-loop.
move data-record to report-line-data
move linage-counter to body-tag
write report-line from report-line-data
end-of-page
add 1 to page-count end-add
move page-count to page-no
move linage-counter to header-tag
write report-line from report-line-header
after advancing page
end-write
end-write
read data-file
at end set endofdata to true
end-read
.
*****************************************************************
* Commentary
* LINAGE is set at a 20 line logical page
* 16 body lines
* 2 top lines
* A footer line at 15 (inside the body count)
* 2 bottom lines
* Build with:
* $ cobc -x -Wall -Wtruncate linage-demo.cob
* Evaluate with:
* $ ./linage-demo
* This will read in linage-demo.cob and produce mini-report
* $ cat -n mini-report
*****************************************************************
END PROGRAM linage-demo.
With those samples, along with Gilbert's answer, you should have enough to tackle your problem, with the caveat that these examples are shy on proper error handling, so be careful is this is homework or a paid assignment. For an example of standard input/output or by filename depending on command line arguments (or lack thereof), see the ocdoc.cob program in the OpenCOBOL FAQ.
Offtopic: Output of an ocdoc pass over ocdoc.cob itself can be seen at http://opencobol.add1tocobol.com/ocdoc.html (Why mention it? The COBOL lexicon highlighter for Pygments has just been accepted into main. Any Pygments pulled after version 1.6 will allow for COBOL (context free) lexical highlighting.)
You write an ordinary Cobol program that reads a file.
The first byte (character) of the record is either 0, 1, 2, or 9.
Define a Working-Storage area (01 level) for each of the 4 record types. Then, after you read the record, you move it from the input area to the appropriate Working-Storage area for the record.
Then you process the record how you wish from one of the 4 Working-Storage areas.
Hello I am total beginner in cobol and needing some homework help. I am trying to write a program that prints address labels on the ouput. But in the output there has to be a header, page number, and date. I have successfully got the program to print the addresses in label format but cannot seem to get the heading line (with the page and date) to show up above it. With my program the way it is there is an error code stating that I have the wrong access mode for the data file. I am unsure what this means. Here is my program. I got rid of the date part just to try and get the heading line in above the addresses. *EDIT: I have added the open and close for "print header out" but now it gives me the error code "file locked" Can anyone shed some light on this.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT LABEL-FILE-IN
ASSIGN TO 'C0603.DAT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT LABEL-FILE-OUT
ASSIGN TO 'C0603.RPT'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT PRINT-HEADER-OUT
ASSIGN TO 'C0603.RPT'
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD LABEL-FILE-IN.
01 LABEL-RECORD-IN.
05 CUST-NAME-IN PIC X(20).
05 ADDRESS-IN PIC X(20).
05 CITY-STATE-ZIP-IN PIC X(20).
FD LABEL-FILE-OUT.
01 LABEL-RECORD-OUT.
05 PRINT-LABEL-OUT PIC X(20).
FD PRINT-HEADER-OUT.
01 REPORT-OUT PIC X(80).
WORKING-STORAGE SECTION.
01 ARE-THERE-MORE-RECORDS PIC X(3) VALUE 'YES'.
01 HEADING-LINE1.
05 PIC X(40) VALUE SPACES.
05 PIC X(12) VALUE
"MAILING LIST".
01 DATE-WS.
05 MONTH-WS PIC XX.
05 YEAR-WS PIC XX.
01 DATE-WS-OUT.
05 PIC X(45) VALUE SPACES.
05 MONTH-WS-OUT PIC XX.
05 VALUE "/".
05 YEAR-WS-OUT PIC XX.
PROCEDURE DIVISION.
000-MAIN-MODULE.
PERFORM 100-INITIALIZATION-MODULE.
PERFORM 200-PROCESS-ONE-RECORD
UNTIL ARE-THERE-MORE-RECORDS = "NO ".
PERFORM 900-TERMINATION-MODULE.
STOP RUN.
100-INITIALIZATION-MODULE.
OPEN OUTPUT PRINT-HEADER-OUT
OPEN INPUT LABEL-FILE-IN
OPEN OUTPUT LABEL-FILE-OUT
ACCEPT DATE-WS FROM DATE.
MOVE MONTH-WS TO MONTH-WS-OUT.
MOVE YEAR-WS TO YEAR-WS-OUT.
PERFORM 600-READ-MODULE.
PERFORM 300-TOP-OF-PAGE-MODULE.
200-PROCESS-ONE-RECORD.
MOVE SPACES TO PRINT-LABEL-OUT
MOVE CUST-NAME-IN TO PRINT-LABEL-OUT
WRITE LABEL-RECORD-OUT
MOVE ADDRESS-IN TO PRINT-LABEL-OUT
WRITE LABEL-RECORD-OUT
MOVE CITY-STATE-ZIP-IN TO PRINT-LABEL-OUT
WRITE LABEL-RECORD-OUT
PERFORM 600-READ-MODULE.
300-TOP-OF-PAGE-MODULE.
MOVE HEADING-LINE1 TO REPORT-OUT.
WRITE REPORT-OUT AFTER ADVANCING 9 LINES.
MOVE DATE-WS-OUT TO REPORT-OUT.
WRITE REPORT-OUT AFTER ADVANCING 1 LINES.
600-READ-MODULE.
READ LABEL-FILE-IN
AT END MOVE "NO " TO ARE-THERE-MORE-RECORDS
END-READ.
900-TERMINATION-MODULE.
CLOSE PRINT-HEADER-OUT.
CLOSE LABEL-FILE-IN.
CLOSE LABEL-FILE-OUT.
I think the problem you are having is that both LABEL-FILE and HEADER-FILE point to the
same physically file ('C0603.RPT'). You can do this, but only one of them may be open at a time. This is
the source of the "file locked" message when you try to open it a second time under a different
name.
The typical way of doing this is to open one file but have multiple record definitions for
writing to it.
Drop the:
SELECT PRINT-HEADER-OUT
ASSIGN TO 'C0603.RPT'
ORGANIZATION IS LINE SEQUENTIAL.
and change the FD's for LABEL-FILE-OUT to include the Header record...
FD LABEL-FILE-OUT.
01.
05 LABEL-BUFFER PIC X(80).
05 LABEL-RECORD-OUT REDEFINES LABEL-BUFFER.
10 PRINT-LABEL-OUT PIC X(20).
10 PIC X(60).
05 PRINT-HEADER-OUT REDEFINES LABEL-BUFFER.
10 REPORT-OUT PIC X(80).
There are other ways of doing this, but the basic idea is to have an output buffer that is the
at least as big as the largest ouput record andREDEFINE it for multiple usages (LABEL or HEADER).
When writing a label line or header line just use WRITE LABEL-BUFFER and then move SPACES to it
after each write to ensure it gets properly initialized before re-populating any of the subordiante
data items.
The "error code stating that I have the wrong access mode for the data file" is because the PRINT-HEADER-OUT file is not OPEN when you execute the statement WRITE REPORT-OUT. All files must be OPENed before they are used and should always be CLOSEd when you are finished with them.