GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Interview Questions  >  Mainframe  >  COBOL
Go To First  |  Previous Question  |  Next Question 
 COBOL  |  Question 95 of 159    Print  
Write sample program to read a file randomly i.e according to key value.

  
Total Answers and Comments: 3 Last Update: March 15, 2007     Asked by: shrikant gadag 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
February 21, 2007 04:49:45   #1  
tarakeshwarsg        

RE: Write sample program to read a file randomly i.e a...
Procedure Division
open input file-name
read file-name key= key-value
-----------------------
--------------------
------------------
close file-name.


stop run




 
Is this answer useful? Yes | No
March 14, 2007 07:31:45   #2  
vaibhav        

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  
 
Is this answer useful? Yes | No
March 15, 2007 07:32:03   #3  
Priyakk Member Since: March 2007   Contribution: 3    

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.
 


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape