how to fetch the record before the last record in a cobol
file( its a huge file and if the key field is not known)
Answers were Sorted based on User's Feedback
Answer / lakshmi
1) iN WORKING STORAGE SECTION, Declare 2 variables HOLD-REC
and PRV-REC Equal to length of file and initialize them to
spaces
2) In Procedure divison, use the below logic and at the end
of the file read, PRV-REC contains the record before the
last record
READ <INFILE>
NOT AT END
MOVE HOLD-REC TO PRV-REC
MOVE INREC TO HOLD-REC
AT END
MOVE 'Y' TO EOF
END-READ
Is This Answer Correct ? | 8 Yes | 1 No |
Answer / mailid
1.Read the file sequentially
2.For every successful read, move the key field to temp variable
3.when EOF is reached, A separate section should be performed in which Key field should not be moved to temp variable, Now, the temp variable will have key of the previously read record.
Using this, we can do a keyed read on the file to fetch the last but one record.
Is This Answer Correct ? | 5 Yes | 6 No |
Answer / logeshwaran ravi
DATA DIVISION.
FILE SECTION.
FD WS-INFILE.
01 WS-INREC.
.
.
.
.
FD WS-OUTFILE.
01 WS-OUTREC.
.
.
.
.
WORKING-STORAGE SECTION.
77 INFS PIC XX.
77 OUTFS PIC XX.
77 CONT PIC 9(3).
77 NUM PIC 9(3).
PROCEDURE DIVISION.
001-OPEN-PARA.
OPEN INPUT WS-INFILE.
OPEN OUTPUT WS-OUTFILE.
002-READ-PARA.
READ WS-INFILE AT END PERFORM 003-WRITE-PARA.
COMPUTE CONT = CONT + 1.
003-WRITE-PARA.
COMPUTE CONT = CONT - 1.
READ WS-INFILE AT END PERFORM 009-CLOSE-PARA.
NUM = NUM + 1.
IF NUM = CONT THEN
MOVE WS-INREC TO WS-OUTREC
WRITE WS-OUTFILE
PERFORM 009-CLOSE-PARA
END-IF.
009-CLOSE-PARA.
CLOSE WS-INFILE WS-OUTFILE.
STOP RUN.
Is This Answer Correct ? | 2 Yes | 5 No |
Answer / vijay kumar reddy
identification division.
program-id. -------
environment division.
file control.
select infile assign to disky.
organization is sequential.
acess mode is sequential.
select outfile assign to disky1.
organization is sequential.
acess mode is sequential.
data division.
file section.
fd 01 infile.
05 ---
05
fd 01 outfile.
05 ---
05
working-storager section.
77 ws-eof pic 9 value zero.
77 ws-buffer1 pic x(80) value spaces.
77 ws-buffer2 pic x(80) value spaces.
procedure division.
000-main-para.
100-open-para thru 100-exit-para.
200-read-para thru 200-exit-para until ws-eof=1.
300-close-para thru 300-exit-para.
stop run.
100-open-para.
open infile.
open outfile.
100-exit-para.
exit.
200-read-para.
at end.
move 1 to ws-eof.
display buffer1.
not at end.
move buffer2 to buffer1
read infile.
move inrec to buffer2.
read infile.
move inrec to buffer1.
200-exit-para.
exit.
300-close-para.
close infile.
close outfile.
300-exit-para.
exit.
Is This Answer Correct ? | 1 Yes | 4 No |
Answer / krishna
<PROCEDURE DIVISION>
OPEN INPUT <INFILE>.
OPEN OUTPUT <OUTFILE>.
PERFORM <READ-PARA-NAME> UNTILL EOF='Y'.
PERFORM <CLOSE-PARA-NAME>.
================================
READ-PARA-NAME.
READ <INFILE> AT END MOVE 'Y' TO EOF
IF EOF NOT = 'Y'
MOVE INREC TO OUTREC
WRITE OUTREC.
================================
CLOSE-PARA-NAME.
CLOSE INFILE.
CLOSE OUTFILE.
STOP RUN.
Is This Answer Correct ? | 4 Yes | 7 No |
What is the difference between copy and include in cobol?
What is inspect in cobol ?
We are using the searching a table which is indexed, once the key is found, how can we get the occurance at which the key was found.
What is the difference between Global and External Variables?
How can we find out wether to declare the data items like Integer, Char,Comp? If comp types how can we decide wether it is Comp and Comp3.How it is? Please Explain... Cheers.
what is index and how to use two tables using index?
tell me about examine inspect and evaluate ?
if someone is using my file,how can i find which user id is using?
Mention the guidelines to write a structured cobol program?
For rewrite, why is it mandatory that file needs to be opened?
what is the use of filler in cobol programing?
What is a scope terminator? Give examples.