RE: Write sample program to read a file randomly i.e a...
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
RE: Write sample program to read a file randomly i.e a...
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.