Page 1 of 2

How to check if file is empty in cobol without reading???

PostPosted: Sat Dec 24, 2011 1:38 pm
by Dilip_M_G
Please dnt answer saying check for file status '10'. I'm not reading in this case. If the file is opened status is 00 but hw can prgm detect for empty file?/

Re: How to check if file is empty in cobol without reading??

PostPosted: Sat Dec 24, 2011 3:01 pm
by prino

Re: How to check if file is empty in cobol without reading??

PostPosted: Sat Dec 24, 2011 3:07 pm
by prino
Of course you can also ask a colleague to read the file every time you need to check if it's empty, and send you an email with the result.

Re: How to check if file is empty in cobol without reading??

PostPosted: Sat Dec 24, 2011 3:19 pm
by Dilip_M_G
Hi Prin,

the requirement is i have to read the PS file for date.. the file contains only date. In case if that doesnt contain, the job should abend. N worst case is i have to read two PS files for date. If either of those is empty the job should abend..

Re: How to check if file is empty in cobol without reading??

PostPosted: Sat Dec 24, 2011 3:53 pm
by BillyBoyo
Prino is being ironic. Without reading a file, you can't tell whether it is empty. OK, you could probably go to some considerable lengths to do so, but I've never found it necessary.

Now, what is your problem? You open the file. You read it. If you get EOF you abend. If not, validate the date. If invalid abend. Read the file again. If not EOF, abend. Close the file.

If you need to do this with date from one file or another, just set an indicator for the EOF abend. Do the same processing for the second file (different indicator) and abend if either is set.

Re: How to check if file is empty in cobol without reading??

PostPosted: Sat Dec 24, 2011 6:17 pm
by steve-myers
BillyBoyo wrote:... You open the file. You read it. If you get EOF you abend. If not, validate the date. If invalid abend. Read the file again. If not EOF, abend. Close the file.
Actually, that method is not reliable. An empty dataset may not have an EOF at its start; it may have a record from the previous user of the data area, or there, quite literally, be nothing there if the dataset is allocated on DASD that has recently been initialized.

I know how ISPF makes this determination, but ISPF is Assembler and has methods available to it that are not available to a Cobol program. ISPF does not open the dataset until it has checked if the dataset is empty. I can't think of a method that is available to Cobol and is reliable to make this determination.

Re: How to check if file is empty in cobol without reading??

PostPosted: Sat Dec 24, 2011 6:25 pm
by NicC
Using BillyBoyo's method is the standard way of doing it. But beware Steve's caution. ALWAYS open and close your datasets - even if they are not being read/written this run.

Re: How to check if file is empty in cobol without reading??

PostPosted: Sat Dec 24, 2011 7:26 pm
by BillyBoyo
I suppose dealing with a dataset with no valid content is a slightly different thing. Often you'll just get some sort of error from the i/o for conflict in RECFM or LRECL.

I don't like datasets which can't have their contents verified. Header record with logical file name, logical date for the data. Data records, which can be sequence-checked if they should be in sequence. Trailer record with record-count and hash-total(s).

Check the header, check that there is only one header. Check the trailer and check there is only one trailer. Doing this I've never processed an incorrect input file. And yes, I have had duplicate trailers happen (through error, obviously). It takes nothing to code the extra bits, and it may save you a lot of grief on the time that your file is up-the-pole.

That said, the single-record-date-files are the least easy to do. With these, check everything you can (I always check for one record, check to the date file "cycling around" from the previous run, check the record-length, check valid format, check not a holiday, etc). Then if it gets through all that, you'll be unlucky if it is wrong.

With all of the above, on systems I've had any influence over, there is never such a thing as a valid "empty" dataset. There can be one which is logically empty, but that always contains a valid and validated header and trailer, with no data records.

Re: How to check if file is empty in cobol without reading??

PostPosted: Tue Dec 27, 2011 11:50 am
by Dilip_M_G
Hi,

Thanks for all..

I found solution for my qn,,

Let me brief abt this and tel my solution..

My Situation was someting lik below..

File1 has record 'abcd'
file2 has record 'efgh'

JCL had dd1 dsn=file1,disp=shr
dsn=file2,disp=shr

Cobol had select filexyz assign to DD1.

Requirement was i wanted to read filexyz(both files which r in single dd step) and if either file1 or file2 was empty i wanted to include abend step.

Solution.

01 ws-test
05 ws-file-data PIC X(4).

01 ws-8byte
05 ws-1st4data pic x(4).
05 ws-2ND4data pic x(4).

read filexyz to ws-test
move ws-file-data to ws-1st4data
read filexyz to ws-test
move ws-file-data to ws-2nd4data.

if (ws-1st4data = spaces or ws-2nd4data = spaces)
display 'abend-- file empty'

Re: How to check if file is empty in cobol without reading??

PostPosted: Tue Dec 27, 2011 2:18 pm
by BillyBoyo
Have you got something like this in your JCL?

//FILE1 DD DSN=date.dataset.one,DISP=SHR
//      DD DSN=date.dataset.two,DISP=SHR


As far as I know, there is no useful way to tell which dataset a particular record came from. If "one" is empty, your first read will get the data from "two" and will then get EOF passed to you when "two" is read (the second time your filename is read in the Cobol program).

The solution for the above JCL is to count how many reads are successful. If there are zero, one or more than two records, abend. If there are exactly two, you have to "hope" they come from different datasets.

"Hoping" isn't much good in computing.

So, make them seperate DD names in the JCL. Checking file-status and processing EOF for your files, count the records as you read them. If there are zero or more than one records, abend, for each of the two date files in the program.

Read through all of the above postings to be aware of some of the issues and resolutions. Bear in mind we had no idea you were talking about "concatenation". Next time, please be more clear.