Page 1 of 1

How to append to an existing sequential file using routine?

Posted: Fri Apr 18, 2008 3:30 am
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

Posted: Fri Apr 18, 2008 3:37 am
by BugFree
You would have to use Status() function to go to the end of file. Look into OpenSeq(), ReadSeq(), and Status() functions.

Posted: Fri Apr 18, 2008 3:43 am
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


Posted: Fri Apr 18, 2008 4:02 am
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.

Posted: Fri Apr 18, 2008 5:55 am
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?

Posted: Fri Apr 18, 2008 6:02 am
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