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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

COBOL Header Problem

Status
Not open for further replies.

rajesh082

Programmer
Dec 18, 2007
38
DK
Hello,

I am writing some records in a file.
The file is declared as follows:

Code:
input-output section.                    
file-control.                            
    select CUSTFILE assign to CUSTFILE   
    organization is sequential           
    file status is ws-cust-status.       

 data division.                                                      
 file section.                                                       
FD CUSTFILE.                                        
01 customer-rec.                                    
-INC RHBAL03C    

working-storage section.     
01 header-rec.                                                 
   10 rec-id                    pic  x(011) value spaces.      
   10 offbal-date               pic  x(008) value spaces.      
   10 prod-system-id            pic  x(008) value spaces.      
   10 rec-content               pic  x(016) value spaces.      
   10 length-chk                pic  x(037) value spaces.

The data record structure is as folows:
Code:
/* Eksternal Customer Records to GFS/KRS - Length : 80      
       3 Account-Id         pic x(014).                     
/* HandelsId                                                
       3 Customer-Id        pic x(010).                     
/* KNID                                                     
       3 Rolle-id           pic x(002).                     
/* Rolle-id                                                 
       3 filler             pic x(054) value spaces.        
/* KobSalg

I first write the header record in the file, and then the data records.
Code:
move 'STARTRECORD'                                  
  to rec-id                    in header-rec        
move prokort-date                                   
  to offbal-date               in header-rec        
move 'RDOFFER'                                      
  to prod-system-id            in header-rec        
move 'OFFBALCUSTREC'                                
  to rec-content               in header-rec        
move length                    of customer-rec      
  to length-chk                in header-rec        
write customer-rec             from header-rec      
initialize header-rec                       

perform varying i from 1 by 1 until i > idx1                       
   perform varying j from 1 by 1 until( j >= 20 or                 
      rolle-nr                    in ws-accounts-rec(i, j)         
                                  = spaces)                        
      add +1                      to idx3                          
      move account-id             in ws-accounts-rec(i)            
        to account-id             in customer-rec                  
      move customer-id2           in ws-accounts-rec(i, j)         
        to customer-id            in customer-rec                  
      move rolle-nr               in ws-accounts-rec(i, j)         
        to rolle-id               in customer-rec                  
                                                                   
      write customer-rec                                           
      initialize customer-rec                                      
   end-perform                                                     
end-perform

In the JCL, the file has been defined as:
//CUSTFILE DD DSN=B96543.TEST.XXX2,
// RECFM=FB,BLKSIZE=800,LRECL=80,
// SPACE=(TRK,(3,30),RLSE),UNIT=DISK,DISP=(NEW,CATLG,DELETE)

When I see the output file, I see that the data records are being written correctly.
But after every 50th record I can see that the header is getting overlapped.

I know that half of the problem is that I should move spaces to the accounts-record after the write so that the filler does have spaces.
However, I would like to understand what am I missing here.

I am not sure why is this happening. Has anybody here experienced this problem before?
This is the closest I could find it on internet: Link
 
why don't you try
Code:
input-output section.
file-control.
    select CUSTFILE assign to CUSTFILE
    organization is sequential
    file status is ws-cust-status.

 data division.
 file section.
FD CUSTFILE.
01 customer-rec [COLOR=#CC0000]pic x(80).[/color]

working-storage section.
[COLOR=#CC0000]01 detail-rec.
-INC RHBAL03C[/color]
01 header-rec.
   10 rec-id                    pic  x(011) value spaces.
   10 offbal-date               pic  x(008) value spaces.
   10 prod-system-id            pic  x(008) value spaces.
   10 rec-content               pic  x(016) value spaces.
   10 length-chk                pic  x(037) value spaces.
move 'STARTRECORD'
  to rec-id                    in header-rec
move prokort-date
  to offbal-date               in header-rec
move 'RDOFFER'
  to prod-system-id            in header-rec
move 'OFFBALCUSTREC'
  to rec-content               in header-rec
move length                    of customer-rec
  to length-chk                in header-rec
write customer-rec             from header-rec
initialize header-rec
[COLOR=#CC0000]move spaces to detail-rec[/color]

perform varying i from 1 by 1 until i > idx1
   perform varying j from 1 by 1 until( j >= 20 or
      rolle-nr                    in ws-accounts-rec(i, j)
                                  = spaces)
      add +1                      to idx3
      move account-id             in ws-accounts-rec(i)
        to account-id             in [COLOR=#CC0000]detail-rec[/color]
      move customer-id2           in ws-accounts-rec(i, j)
        to customer-id            in [COLOR=#CC0000]detail-rec[/color]
      move rolle-nr               in ws-accounts-rec(i, j)
        to rolle-id               in [COLOR=#CC0000]detail-rec[/color]

      write customer-rec [COLOR=#CC0000]from detail-rec[/color]
      initialize customer-rec
   end-perform
end-perform

it is a standard I (and others) have of always defining the output record as a single field pic x, and issue a write xx from ...

with regards to the why the link you posted should give some indication of why - maybe one of the other guys here knows.

Regards

Frederico Fonseca
SysSoft Integrated Ltd

FAQ219-2884
FAQ181-2886
 
Hi Frederico,

I agree with you; If I move spaces to customer-rec after every write the problem will be fixed.
However I want to understand why this is happening.

This is how the records look now
Code:
STARTRECORD        RDOFFER OFFBALCUSTREC   000000080   
10001703004   812978119074

The 51th record (50th detail + 1 header in file) looks like this
Code:
10768680043   802150344575 OFFBALCUSTREC   000000080

After every block (multiplied by 5 default QSAM buffers) in the debugger I can see that after the Write command the Customer-REC buffer is getting filled up. This fills the customer-rec with the actual header, which is then overwritten with the detail record (account-id and customer--id).

This is damn confusing.

 
A simple answer is that the records defined in the FD section are implemented as a pointer and for QSAM files a Write (and also a read) is simply a pointer update

i.e

[pre] File Buffer
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
^
! initial FD Record

After a Read / Write the FD pointer moves to the next record.
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
^
! FD Record

After next Read / Write the FD pointer moves to the next record.
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
^
! FD Record
[/pre]
When the buffer is filled, it gets written to disk.

This is a very simplistic explanation, what actually happens depends on file type, compile options etc
 
is it an idea perhaps to define the record in working-storage with the right defaults and use write from?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top