How to append to an existing sequential file using routine?

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
pradeepkumar_b@thbs.com
Participant
Posts: 28
Joined: Thu Mar 13, 2008 11:24 pm

How to append to an existing sequential file using routine?

Post by pradeepkumar_b@thbs.com »

I am trying to write a message to log file(sequential file) using a server routine which called as after job subroutine. I want to append log to an existing file. how can i achieve this? I tried with combination of openpath & write but it only overwrites. Help needed..pls...

thanx in advance
Pradeep
BugFree
Participant
Posts: 82
Joined: Wed Dec 13, 2006 6:02 am

Post by BugFree »

You would have to use Status() function to go to the end of file. Look into OpenSeq(), ReadSeq(), and Status() functions.
Ping me if I am wrong...
BugFree
Participant
Posts: 82
Joined: Wed Dec 13, 2006 6:02 am

Post by BugFree »

This will work.

Code: Select all

OPENSEQ FilePath:FileName TO LogFile ON ERROR Ans = -1
THEN 
    LOOP                                                          
              READSEQ DummyVar FROM LogFile                             
              THEN                                                      
                  stat=STATUS() 
               END
               ELSE                                                      
                  EXIT                                                  
               END
    WHILE stat<>1 REPEAT
WRITESEQ

Ping me if I am wrong...
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Yuk!

Use SEEK to get to end of file quickly, don't read through the file.

OpenSeq can take its Else clause even if the file is open for writing - you must also accommodate this possbility.

Search the forum for a Routine called OpenSequentialFile that takes all the hard work out of it.

And you must, must, must use CloseSeq to close a file opened for sequential access with OpenSeq, so that the exclusive lock set by OpenSeq can be released.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
BugFree
Participant
Posts: 82
Joined: Wed Dec 13, 2006 6:02 am

Post by BugFree »

Ok. Here is the modified code.

Code: Select all

OpenSeq FilePath : FileName To TempFile On Error Ans = "Not Able to open file"
Then
   Seek TempFile , 0, 2
   THEN
      WriteSeq Message to TempFile Then Ans = "Success"
   End
End
CloseSeq TempFile
Yuk? :oops: Ray I am still a beginner. Anyway thanks for the pointer. Any more Yuks, improvements?
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Code: Select all

OpenSeq FilePath : FileName To TempFile 
On Error 
   Ans = "Error (code " : Status() : ") opening file." 
End
Locked
   Ans = "File in use by user number " : Status()
End
Then 
   * File exists and opened.  Need to position to EOF.
   Seek TempFile , 0, 2 
   Then 
      WriteSeq Message to TempFile Then Ans = 0 
   End 
End 
Else
   If Status() = 0
   Then
      * File does not exist but is open for writing.
      * The write operation will bring the file into existence.
      WriteSeq Message To TempFile Then Ans = 0
   End
End
If FileInfo(TempFile,0) Then CloseSeq TempFile 
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Post Reply