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

What is the difference between copy and include in cobol?

1 Answers  


What is inspect in cobol ?

0 Answers   Infosys,


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.

1 Answers  


What is the difference between Global and External Variables?

0 Answers  


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.

1 Answers   Syntel,






what is index and how to use two tables using index?

1 Answers  


tell me about examine inspect and evaluate ?

2 Answers   CTS,


if someone is using my file,how can i find which user id is using?

4 Answers  


Mention the guidelines to write a structured cobol program?

0 Answers  


For rewrite, why is it mandatory that file needs to be opened?

0 Answers  


what is the use of filler in cobol programing?

4 Answers   MAT,


What is a scope terminator? Give examples.

2 Answers  


Categories