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



how to fetch the record before the last record in a cobol file( its a huge file and if the key fiel..

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

how to fetch the record before the last record in a cobol file( its a huge file and if the key fiel..

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

how to fetch the record before the last record in a cobol file( its a huge file and if the key fiel..

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

how to fetch the record before the last record in a cobol file( its a huge file and if the key fiel..

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

how to fetch the record before the last record in a cobol file( its a huge file and if the key fiel..

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

Post New Answer

More COBOL Interview Questions

Write a program that uses move corresponding.

0 Answers  


What is the utilization of copybook in cobol? Could we utilize a similar copybook?

0 Answers  


what is amode(24), amode(31), rmode(24) and rmode(any)?

0 Answers  


What is the usage of comp fields in cobol?

0 Answers  


Can we change the password using ALTER? anyone tried and changed?

0 Answers   IBM,


wht is the use of evalute verb ? how do u declare recfm in cobol and jcl ?

2 Answers   EDS,


can i use multiple when statements in search & search all ? justify ur answer?

2 Answers  


How to concatenation one or more string?

4 Answers   Temenos,


How to print 10 to 1 if the input have only 10 digit number?

0 Answers  


What are the differences bitween cobol and cobol-2?

1 Answers   Wipro,


The below is the declaration for a variable ws 01 ws pic 9(3). if you want to insert space how will you do that. in which level u should do it

3 Answers   ADP,


What is the difference between PIC 9.99 and 9v99?

7 Answers  


Categories