Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identifying if file is empty. 1

Status
Not open for further replies.

gust1480

Programmer
Mar 19, 2002
148
PH
In a sequential access mode I use this code to determine whether a file is empty :

READ ITEM-FILE INTO IFILE-WS
AT END MOVE 1 TO EOF-SW.

IF EOF-SW = 0 * IT MEANS THAT IT IS NOT EMPTY
PERFORM .........
ELSE
DISPLAY 'FILE IS EMPTY'
END-IF.

STOP RUN.

How will I determine if a file is empty in a random access mode?
 
Create a second FD to the same physical file and add the ACCESS MODE IS SEQUENTIAL clause. With this FD you can do the same as you wrote for the sequential file.
Or, use API-calls to determine the file size, if it is zero or the size of the file header (if the Cobol system is using file headers) the file is empty.

Marcel
 
When you try top open an empty indexed file (i.e., a VSAM file), your going to get a '90' return code. Your program needs to trap the error and respond accordingly.
 
Hi Dhecht,

You're right on one count, when the VSAM file has not been loaded. If all the recs have been deleted from the VSAM file, you have another situation.

In the one situation the file is considered "unloaded", in the other it's considered "empty". An "empty" VSAM file can be successfully opened; an "unloaded" VSAM file will fail on an open other than OPEN OUTPUT.

Regards, Jack.
 
Hi! Dhecht,
How does a return code works anyway? does it immediately have a value 90 when I open it or do I have to pass that value?
 
Simple example with a line sequential file.

FILE-CONTROL.
SELECT MADLNK ASSIGN TO EXTERNAL OMADLNK
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS400-FILE-STATUS.

In Working Storage
01 WS400-FILE-STATUS-AREA.
03 WS400-FILE-STATUS PIC XX VALUE SPACES.
88 WS400-FILE-OK VALUE '00'.


You query the file status after having done an operation on the file. In this case WS400-FILE-STATUS. This will hold the return code immediately AFTER you have done your open.
 
Hi! Greg,
How about in a VSAM file> Will that also work?
 
Hi gus,

Yes, that will work for VSAM, but change the ORG to INDEXED.
Also, you have a choice of ACCESS MODEs - RANDOM, SEQUENTIAL, or DYNAMIC. I think SEQ is the default.

Now for the OPEN:

When a VSAM file defined as access RANDOM or DYNAMIC, is OPENed for INPUT or I-O and there were NEVER any records in it, a code of '90' is put into the FILE STATUS field and control is returned to your pgm and the stmt immediately following the OPEN stmt will be executed. This means that the OPEN was unsuccessful and any further attempts to access the file will fail.

If, on the other hand, it was defined as SEQUENTIAL or DYNAMIC and it was OPENed for OUTPUT, You would be preparing to load the file with data and the OPEN returns a code of '00'. Subsequent WRITEs to the file will be successful (God willing).

If, on the other other hand, the VSAM file contained records in the past that were deleted , leaving it with no records, you could define it with any ACCESS MODE and you could open it successfully for INPUT, I-O, EXTEND, even OUTPUT if you wanted to reload it. The return code is zero.

All of this still doesn’t answer your ques, but you should understand it if you want to understand the answer. The answer is ......

First you have to determine what kind of “empty” file you have, second, you have to decide what you want to do in each case.

You can define it ACCESS DYNAMIC, OPEN it for INPUT, If the file had records but is now “empty”, the FILE STATUS field s/b zero, then READ NEXT EOF etc. If EOF there are no records in the file, if not, you have records in the file and you can do direct READs (READs w/no NEXT) to process the file.

But suppose you get a ‘90’ on OPEN, what do you want to do?

There may be other approaches, stay tuned.

Regards, Jack.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top