Write sample program to read a file randomly i.e according to key value.

Showing Answers 1 - 9 of 9 Answers

tarakeshwarsg

  • Feb 21st, 2007
 

Procedure Division
open input file-name
read file-name key= key-value
-----------------------
--------------------
------------------
close file-name.


stop run



  Was this answer useful?  Yes

vaibhav

  • Mar 14th, 2007
 

                 first of all your file should have uniquely defined key field . declare a  appropriate cobol variable depending on key field.  move the key value into the field issue " read invalid key " syntax  

  Was this answer useful?  Yes

Priyakk

  • Mar 15th, 2007
 

If a file must be read randomly, then it must either be a relative file or a direct file. In either cases we make use of a key.(relative key or a record key to access records).
steps to read a file randomly.
1. Have a file which is defined to be accessed in a random way in the IO Section and have a field (In case of indexed files, the key must be a part of the record.In case of a relative file the key must be from the Working Storage Section).
2. In Procedure Division, Open the I/P file.
3.To assign values to the key, we can do any of the foll.
    a) ACCEPT the value from the user and MOVE it to the keyfield.
    b) Assign any values directly to the keyfield  using MOVE.
4. READ filename RECORD 
                INVALID KEY GOTO ERROR-PARA.  (or something like that)
5.Steps 3 to 4 must be put in a loop to read more than one record.
NOTE: 
1)Only those records whose keyfields are present in the I/P file (Indexed file)    can be accessed else READ will be unsuccessful.
2)In case of Relative files Relative record no starts from 1 to 65536(or something) depending on the no of records present in the I/P file.
 

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions