Mar 21, 2010

File Handling

1. Explain how files are handled in COBOL program?

Files must be declared in in INPUT-OUTPUT SECTION (In ENVOIRNMENT DIVISION) under FILE CONTROL.Logical file name is assgined using SELECT clause to file. Also organization (Whether indexed, sequential) and access mode (Seqential,random, dynamic) specified here.

Then in FILE SECTION of DATA DIVISION,  FD  file descriptor is specified for the file where record length , type of record (FB,VB) is specified along with record layout.

Then in prodcedure division file is opened as input,input-output,output mode.

records can be read using READ and can be written using WRITE/REWRITE statement.

Files can be closed using CLOSE statment in PROCEDURE DIVISION.

2. A MASTER-FILE has been opened in I-O mode.Following are the records in the file.

CAPGEMINI, PUNE
ACCENTURE,PUNE
INFOSYS,PUNE
COGNIZAT,PUNE.

And following COBOL statements are exected in procedure division.

READ MASTER-FILE
READ MASTER-FILE
MOVE 'WIPRO,PUNE' TO MASTER-REC
WRITE MASTER-FILE FROM MASTER-REC.

Then what will be the contents of the file?
Since MASTER-FILE has been opened in I-O mode and WRITE statement is trying to write in the same file, we will get invalid file status and no WRITE operations will be performed.
Files which are delcared as I-O, only REWRITE statment can write the records.
Hence contenets of the file will be same.



3. A MASTER-FILE has been opened in I-O mode.Following are the records in the file.




CAPGEMINI, PUNE
ACCENTURE,PUNE
INFOSYS,PUNE
COGNIZAT,PUNE.


And following COBOL statements are exected in procedure division.


READ MASTER-FILE
READ MASTER-FILE
MOVE 'WIPRO,PUNE' TO MASTER-REC
REWRITE MASTER-FILE FROM MASTER-REC.
REWRITE MASTER-FILE FROM MASTER-REC.


Then what will be the contents of the file?
After execution of above statements of contents of the file will be


CAPGEMINI, PUNE

WIPRO,PUNE
INFOSYS,PUNE
COGNIZAT,PUNE.


File has been opened in input output mode. And first two records are read. After that pointer now points to the second record in the file.
Now if REWRITE operation is performed, pointer will point to the second record and second record will be overwritten. After rewrite, pointer will still point to the second record in the file and same record will be overwritten.

 4. Program A has opened a file in output mode. Program A calls Program B. Since file has already opened in Program A, can program B direcly write into that file?

If program B has to write into the files, it cannot directly write into the file.

In both Programs Prog A and Prog B, file should be defined as external in FD. Also in both the programs, file should be defined in FILE CONTROL using SELECT clause.
PROG A opens a file and calls prog B. Since file is already opened in PROG A, no open statement required in PROG B.
And Then Prog B can write into the file.

5. If records needs to be deleted from a ps file, Which one of the following verbs can be used?


DELETE
ERASE

None of the above verbs can be used to delete the record from the PS file.

6. Can 88 levels be defined in 01 level under FD?88 Levels can be defined under 01 - file layout under FD.

7. In a physically sequential file, after reading 100 records , if again first records needs to be read, then which one of the following needs to be done?

A.Address of the first record from a file needs to be stored into pointer using ADDRESS OF keyword.
   After reading 100 records, point to the address stored into pointer using SET verb.
   In this way records can be read from the start again.

B. Close the file, OPEN it again and read it from the start.

COBOL does not support address handling for the files. so first option is not possible.
Second option is easy. Since it is sequential file and records need to be read sequentially, closing the file, oepning it and reading the records from the start is the only option available.

8. Can COPY statement is used to copy all the content of one file to another file?
 COPY statement cannot be used for file handling. If all the conetnts from one file needs to be copied to another file, the source file needs to be opened in INPUT mode and target file needs to be opened as OUTPUT.

Records from INPUT file needs to be read one by one and at a same time, those should be written in OUTPUT file.

9.  If ORGANIZATION is defined as SEQUENTIAL and ACCESS MODE is defined as INDEXED in SELECT clasue, then the file must be one of these -> KSDS, RRDS,LDS,ESDS,PS?
If ORGANIZATION IS SEQUENTIAL then the ACCESS MODE must be SEQUENTIAL ONLY. And these properties are for PS file only.

For KSDS, ORGANIZATION should be INDEXED and access mode can be defined as SEQUENTIAL, RANDOM, DYNAMIC.

For ESDS, ORGANIZATION should be sequential and access mode can be defined as sequential.

for RRDS, ORGANIZATION should be relative and access mode can be defined as SEQUENTIAL, RANDOM, DYNAMIC.


10. If the records need to be written at the end of file, then how it can be done?

If the records needs to be added at the end of file, then files should be opened in EXTEND mode.

11. How will you define your record descriptions in the FILE SECTION if you want to use three different record descriptions for the same file?


FD filename
DATA RECORDS ARE rd01, rd02, rd03.
01 rd01 PIC X(n).
01 rd02 PIC X(n).
01 rd03 PIC X(n).



                                                                          BACK TO COBOL

6 comments:

  1. Very good questions...It is application based and not theoretical..
    Please post few more this way...

    ReplyDelete
  2. really nice questions can please send us some more with explanations like this

    ReplyDelete
  3. really nice questions can please send us some more with explanations like this

    ReplyDelete
  4. Really helpful. thanks for the detailed information.

    ReplyDelete
  5. Very good questions...It is application based and not theoretical..
    Please post few more this way...

    ReplyDelete