How to create a sequential file?
Moderators: chulett, rschirm, roy
-
- Participant
- Posts: 18
- Joined: Mon Nov 25, 2002 3:23 am
- Location: Switzerland
How to create a sequential file?
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
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
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}
OPENSEQ 'filepath' TO FilePtr THEN {if if already exists} ELSE {if it doesn't}
Last edited by ArndW on Thu Mar 10, 2005 9:51 am, edited 1 time in total.
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
Kenneth Bland
Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
-
- Participant
- Posts: 18
- Joined: Mon Nov 25, 2002 3:23 am
- Location: Switzerland
Re: How to create a sequential file?
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
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
Ken,kcbland wrote:Actually, Arnd is wrong, you use the CREATE command ...
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.
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 -
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.![Smile :-)](./images/smilies/icon_smile.gif)
Regards,
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
Thanks for pointing out an alternate method.
![Smile :-)](./images/smilies/icon_smile.gif)
Regards,
Mike Hester
mhester@petra-ps.com
mhester@petra-ps.com
Okay,ArndW wrote:can we agree that I'm not as right as you
![Embarassed :oops:](./images/smilies/icon_redface.gif)
![Embarassed :oops:](./images/smilies/icon_redface.gif)
Kenneth Bland
Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
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.
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.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
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
Try doing this at the top of the logic to demonstrate how it works:
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.
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"
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.
Kenneth Bland
Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle