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



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

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

Postby Dilip_M_G » Sat Dec 24, 2011 1:38 pm

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?/
Dilip_M_G
 
Posts: 12
Joined: Wed Nov 02, 2011 10:45 am
Has thanked: 0 time
Been thanked: 0 time

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

 

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

Postby prino » Sat Dec 24, 2011 3:01 pm

Robert AH Prins
robert.ah.prins @ the.17+Gb.Google thingy
User avatar
prino
 
Posts: 536
Joined: Wed Mar 11, 2009 12:22 am
Location: Oostende, Belgium
Has thanked: 3 times
Been thanked: 21 times

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

Postby prino » Sat Dec 24, 2011 3:07 pm

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.
Robert AH Prins
robert.ah.prins @ the.17+Gb.Google thingy
User avatar
prino
 
Posts: 536
Joined: Wed Mar 11, 2009 12:22 am
Location: Oostende, Belgium
Has thanked: 3 times
Been thanked: 21 times

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

Postby Dilip_M_G » Sat Dec 24, 2011 3:19 pm

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..
Dilip_M_G
 
Posts: 12
Joined: Wed Nov 02, 2011 10:45 am
Has thanked: 0 time
Been thanked: 0 time

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

Postby BillyBoyo » Sat Dec 24, 2011 3:53 pm

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.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

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

Postby steve-myers » Sat Dec 24, 2011 6:17 pm

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.
steve-myers
Global moderator
 
Posts: 1886
Joined: Thu Jun 03, 2010 6:21 pm
Has thanked: 4 times
Been thanked: 197 times

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

Postby NicC » Sat Dec 24, 2011 6:25 pm

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.
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator
 
Posts: 2690
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisys (almost)
Has thanked: 4 times
Been thanked: 105 times

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

Postby BillyBoyo » Sat Dec 24, 2011 7:26 pm

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.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

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

Postby Dilip_M_G » Tue Dec 27, 2011 11:50 am

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'
Dilip_M_G
 
Posts: 12
Joined: Wed Nov 02, 2011 10:45 am
Has thanked: 0 time
Been thanked: 0 time

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

Postby BillyBoyo » Tue Dec 27, 2011 2:18 pm

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.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

Next

Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post