Page 1 of 1

How to create a sequential file?

Posted: Wed Mar 09, 2005 10:46 am
by JeanPierreHaddad
Hello everyone

I am trying to figure out what is the BASIC instruction to create a sequential file that I could read with an OpenSeq and ReadSeq.

Thanks in advance

Posted: Wed Mar 09, 2005 10:57 am
by ArndW
The OPENSEQ command will create the file if it does not already exist.

OPENSEQ 'filepath' TO FilePtr THEN {if if already exists} ELSE {if it doesn't}

Posted: Wed Mar 09, 2005 11:46 am
by kcbland
Actually, Arnd is wrong, you use the CREATE command if the file does not exist. If you have all of your data in a dynamic array, you can use this logic to write the dynamic array to a file:

Code: Select all

      txt = "your data you want to write out"
      ValidOpen = @FALSE
      filename = "/path/to/your/filename"
      OPENSEQ filename TO FILE Then
         ValidOpen = @TRUE
      End Else
         CREATE FILE Then ValidOpen = @TRUE
      End
      If NOT(ValidOpen) Then
         Call DSLogFatal("Could not create the file: ":filename, "OutputFile")
      End

      AM.CNT = DCOUNT(txt, @AM)
      For i=1 to AM.CNT
         WRITESEQ txt<i> TO FILE else
            Call DSLogFatal("Could not update the file: ":filename, "OutputFile")
         End
      Next i
      WEOFSEQ FILE

Re: How to create a sequential file?

Posted: Thu Mar 10, 2005 9:16 am
by JeanPierreHaddad
Thanks very much,

Actually the file is created if it does not exist. But I like the 2nd solution where you write the command to write the file. It allows you to check if your file exists (new or old) and has been opened for usage.

Very neat and well structured,

Thanks a lot.

Jean-Pierre

Posted: Thu Mar 10, 2005 9:49 am
by ArndW
kcbland wrote:Actually, Arnd is wrong, you use the CREATE command ...
Ken,

can we agree that I'm not as right as you; but am not wrong? The OPENSEQ on a file that does not exist will create the file but go down the ELSE branch. It is not as clean as using an explicit CREATE but it works, is documented & I've used it for years (I might change to CREATE, though; I haven't used that before). There is also a difference in syntax twixt using OPENSEQ on a path (which is what I showed) and OPENSEQ using FILE/RECORD syntax.

Posted: Thu Mar 10, 2005 10:15 am
by mhester
Arnd,

Thanks for pointing out that you too were correct. I think it's important for all to understand that there are at least two ways to skin this cat and I have been using both methods since 1984. The complete syntax for your method might look something like -

Code: Select all

      OPENSEQ 'C:\XXXXX.TXT' TO FWORK THEN
         PRINT "FILE OPEN"
      END ELSE
         PRINT "CREATING FILE"
      END

     WEOFSEQ FWORK
     CLOSESEQ FWORK
This will do the same thing as what Ken stated, but with much less code. This solution may not include a lot of the error handling, but this should work just fine.

Thanks for pointing out an alternate method. :-)

Regards,

Posted: Thu Mar 10, 2005 11:17 am
by kcbland
ArndW wrote:can we agree that I'm not as right as you
Okay, :oops: , I may be wrong, but I do believe there are reasons to use the CREATE statement for the file to be created, I just can't quite remember right now the reasons there is an explicit statement for doing so. If it's an unnecessary extra step, then I stand corrected. :oops:

Posted: Thu Mar 10, 2005 11:33 am
by ArndW
With some of the PICK type accounts just opening up an file with OPENSEQ will not create an empty file; but since DataStage always creates PI-type accounts it doesn't make a difference for us - but it is better style to explicitly use the statement created for that purpose.

Posted: Thu Mar 10, 2005 12:55 pm
by kduke
I knew there was a reason I did it the way Ken did. I am used to Ultimate flavor.

Posted: Thu Mar 10, 2005 7:07 pm
by ray.wurlod
Technically, OPENSEQ does not create a sequential file. Ever.

Even though the file does not exist, it can be opened. What it actually opened is a communication channel (= file unit) through which the software can interact with the file. OPENSEQ opens the channel, CLOSESEQ closes it. (There's some other hairy stuff around the rotating file pool and whether the file is genuinely open at any point in time, but that's outside the scope of the present discussion.)

If OPENSEQ has been used, and a CLOSESEQ is issued without any operation having written to the file (for example WRITESEQ, WEOFSEQ, WRITEBLK, FLUSH, CREATE), then the file will not come into existence.

If any of these operations has occurred, then the file will come into existence. Buffering may mean that it does not appear until the next sync after CLOSESEQ (unless a non-buffered operation such as WRITESEQF or a write with NOBUF in effect has occurred).

The CREATE statement forces the file to exist, even if nothing is written to it. So, if the design mandates a file to be created, even if zero length, then the CREATE statement is the easiest mechanism to guarantee that.

If there's no requirement to create a file, why bother? Rely on the above rules.

Posted: Fri Mar 11, 2005 11:11 am
by naren6876
How to write each element in the Array in the next(new) line.
kcbland wrote:Actually, Arnd is wrong, you use the CREATE command if the file does not exist. If you have all of your data in a dynamic array, you can use this logic to write the dynamic array to a file:

Code: Select all

      txt = "your data you want to write out"
      ValidOpen = @FALSE
      filename = "/path/to/your/filename"
      OPENSEQ filename TO FILE Then
         ValidOpen = @TRUE
      End Else
         CREATE FILE Then ValidOpen = @TRUE
      End
      If NOT(ValidOpen) Then
         Call DSLogFatal("Could not create the file: ":filename, "OutputFile")
      End

      AM.CNT = DCOUNT(txt, @AM)
      For i=1 to AM.CNT
         WRITESEQ txt<i> TO FILE else
            Call DSLogFatal("Could not update the file: ":filename, "OutputFile")
         End
      Next i
      WEOFSEQ FILE

Posted: Fri Mar 11, 2005 11:33 am
by kcbland
Try doing this at the top of the logic to demonstrate how it works:

Code: Select all

txt = ""
txt<-1> = "this is output line 1"
txt<-1> = "this is output line 2"
txt<-1> = "this is output line 3"
txt<-1> = "this is output line 4"
txt<-1> = "this is output line 5"
txt<-1> = "this is output line 6"
You can use the posted logic in a DS Function, and just pass an array of data like above and a filename as arguments to the function. The function will take care of creating the file and writing the contents.

Now, if you want to APPEND to an existing file, you could improve the function to have an extra argument for append versus overwrite. If that argument says append, you just need a conditional statement to seek to the end of the file.