Ren
Sophomore Member
Retired Mainframer
Posts: 82
|
Post by Ren on Jun 17, 2019 8:06:20 GMT -5
' TW.MACRO Dim outhand, x, outrec As String Uses "File" OutHand = FILE_Open("testback.txt", "OUTPUT") outrec = "Rec One" x = FILE_Put(OutHand, outrec) MSGBOX 0, "x (PUT) =" +x x = FILE_Close(OutHand) halt
With or without an initial instance of "testback.txt", I get a 1 for the open (OutHand), and a 54 on the put, and a 0 on the close. The output file is always empty. Even if it starts as a populated file.
Looking in the ThinBasic documentation, the examples for File_PUT are all the same, and are coded like my statement. No where could I find a list of status codes, nor what a code of 54 means.
Also I could not find a discussion as to the meanings of these values (for the OPEN):
Possible values: "INPUT", "OUTPUT", "APPEND", "BINARY", "RANDOM"
I'm assuming OUTPUT for my statement above, but what is BINARY and RANDOM?
I also tried APPEND with a populated file, that too gets a 54 on the PUT, but the existing data in the dataset is retained. HELP PLEASE.
Other than the Thinbasic.org site, is there any other documentation or examples available for ThinBasic? (all I need is a few working examples, and I'm good). Thanks
|
|
Ren
Sophomore Member
Retired Mainframer
Posts: 82
|
Post by Ren on Jun 17, 2019 9:04:02 GMT -5
So you are saying I should use BINARY rather than OUTPUT? on the OPEN? When does one use OUTPUT, is the a list of these definitions somewhere?
OK I changed from OUTPUT to BINARY. That worked, so I added a 2nd PUT, thinking I'd get a 2nd record. All it did was append the text from the 2nd PUT to the end of the first record. How do I force the 2nd put into the next record?
' TW.MACRO Dim outhand, x, outrec As String Uses "File" OutHand = FILE_Open("testback.txt", "BINARY") outrec = "Rec One" x = FILE_Put(OutHand, outrec) outrec = "Rec Two" x = FILE_Put(OutHand, outrec) x = FILE_Close(OutHand) halt
Thanks
|
|
Ren
Sophomore Member
Retired Mainframer
Posts: 82
|
Post by Ren on Jun 17, 2019 9:50:41 GMT -5
OK, I corrected my DIM statements, (but OUTPUT still does not work, only BINARY), I also discovered that I have to add a "+ $CRLF" to the end of each "outrec". That will give me a new record. So now, if I can figure out how to append a record to the end of file. Just changing BINARY to APPEND doesn't work. WOW, I found another whole section ...FILELINE... The LineInput function reads a line at a time. This is also good for what I'm doing. Except I could not find a FILELINE function for PUT? I just want to build a file update routine. Read an input file, and conditionally write an (updated) output record to a different file. I think I have what I need now. But I would still like to know how to add a record to the end of the file . Thanks.
|
|
|
Post by George on Jun 17, 2019 15:51:35 GMT -5
Ren: Here's one of the new examples coming with the next release. It shows how to write a file using the FILE module.
George
' FMJoin.macro '----- Join selected files and open in a new session '----- If no macro operand, the collected data will be opened '----- in a Clipboard session '----- '----- If an operand, it is assumed to be a filename and the '----- collected data will be created in that file and the file '----- opened in a normal Edit session. '----- '----- Select files with a Z line command (Could use any character) '----- ' Created: George Deluca - June 3, 2019 ' USES "FILE"
'----- Check environment if isfalse Is_FM then halt(8, "FMJoin: Not running in File Manager") if isfalse Is_Primary_Cmd then halt(8, "FMJoin: Not running as Primary Command")
'----- Declare variables dim FCount as long = FMGet_FCount dim i, j as long dim FName, FullName as string = "" ' Filename to null (CLIP mode) dim CB, lText as string = "" ' Set strings to null dim IFile, OFile as DWORD ' File handles
'----- See if an Ouput filename specified, if so, get it's name if Get_Arg_Count > 0 then FName = Get_Arg$(1) ' An operand? Save as FName
'----- Scan for selected files in the list for i = 1 to FCount when FMGet_FType(i) = FM_EQU_File ' Loop through Files
if ucase$(trim$(FMGet_Cmd$(i))) = "Z" then ' A selected file? FullName = FMGet_Path$(i) + FMGet_FileName$(i) ' Build full filename
'----- Open Output if not CLIP mode If FName <> "" and OFile = 0 then ' If O/P file and not opened if instr(FName, "\") = 0 then ' If FName unqualified FName = FMGet_Path$(i) + FName ' Add the Input path end if ' OFile = File_Open(FName, "OUTPUT") ' Open the output If OFile = 0 then Halt(8, "FMJoin: Can't open: " + FName) end if
'----- Open the Input file IFile = File_Open(FullName, "INPUT") ' Open the file If IFile = 0 then Halt(8, "Can't open: " + FullName)
'----- Read a single Input file Do while isfalse File_EOF(IFile) ' Read the data if FName = "" then ' If in CLIP mode CB += File_LineInput(IFile) + $CRLF ' Add to CB string else ' lText = File_LineInput(IFile) ' Read a line File_LinePrint(OFile, lText) ' Write to the output end if ' loop '
'----- Close Input, clear Select character File_Close(IFile) ' FMSet_Cmd(i, " ") ' Clear the selection char incr j ' Count files done end if ' next i ' if FName <> "" then File_Close(OFile) ' Close O/P file if we have one
'----- Did we accomplish anything? if j = 0 then Halt(0, "FMJoin: No files were selected")
'----- Invoke via the Clipboard and CLIP if FName = "" then ' In CLIP mode? ClipBoard_SetText(CB) ' Write to the Clipboard SPF_Post_Do("(Home)(EraseEOL)[CLIP](Enter)") ' Invoke CLIP else '
'----- Or invoke via a new edit session FName = SPF_Quote$(FName) ' Put FName in quotes SPF_Post_Do("(Home)(EraseEOL)[EDIT " + FName + "](Enter)") ' Invoke EDIT end if ' Halt(0) '
|
|
Ren
Sophomore Member
Retired Mainframer
Posts: 82
|
Post by Ren on Jun 19, 2019 14:15:06 GMT -5
Very cool. Thanks.
I have: outfile = File_Open(out_dsn, "BINARY") cc = File_Put(outfile,out_rec) working (as long as I have a $CRLF as the last of "out_rec")
I see you used: OFile = File_Open(FName, "OUTPUT") File_LinePrint(OFile, lText)
I've not tried the "LinePrint" version yet. I wonder if there are any advantages of one over the other...
Thanks again.
|
|
|
Post by George on Jun 19, 2019 14:29:56 GMT -5
Ren: if the data is just normal text data, there's no need or reason to use Binary.
George
|
|
Ren
Sophomore Member
Retired Mainframer
Posts: 82
|
Post by Ren on Jun 21, 2019 7:44:17 GMT -5
Consider the following MACRO please:
Uses "File" Dim OutFile, cc As Number Dim out_rec, in_rec, outdsn as string outdsn = "testtest.txt" OutFile = FILE_Open(outdsn, "BINARY") in_rec = "TEST line 1" out_rec = in_rec +$CRLF cc = FILE_PUT(OutFile, out_rec)
works fine
However using OUTPUT, in the open, rather than BINARY, I get a return of 54 from the PUT
Using FILE_LINEPRINT, in lieu of File_PUT, with OUTPUT, I get two lines (the last one being blank). Removing the $CRLF, it works fine. Using the FILE_LINEPRINT, with BINARY, I get a return code of zero, but no records at all, using the CRLF or not.
So, it looks like the 2 methods to write are:
1) File_PUT with a BINARY OPEN, and $CRLF on each record) 2) FILE_LinePrint with a OUTPUT OPEN (and no $CRLF)
You just can't mix and match apparently.
Thanks,
|
|
|
Post by George on Jun 21, 2019 10:25:48 GMT -5
Exactly, BINARY give the formatting and writing control completely in your hands. The data can be textual, raw data, some kind of DataBase structure etc. It imposes no restrictions at all.
OUTPUT is for normal text type data, that's why it's functions are optimized for text, like adding $CRLFs etc.
You're an old mainframer, it's like the difference between using EXCP, BSAM, QSAM etc. Pick what you need and stick with it, there was no mix and match back then either.
George
|
|
|
Post by mueh on Jun 21, 2019 15:13:59 GMT -5
Hi ! Thinbasic doc states at File_Put Write a string to a file opened in binary mode. File_LinePrint File must be opened for OUTPUT. See FILE_Open. so i think it's working as documented
|
|
|
Post by George on Jun 22, 2019 12:06:36 GMT -5
I Just tried the following and it works perfectly. Except for adding the Close_File etc. it's Ren's code.
' Try1.macro Uses "File" Dim OutFile, cc As Number Dim out_rec, in_rec, outdsn as string outdsn = "testwrite.txt" OutFile = FILE_Open(outdsn, "BINARY") in_rec = "TEST line 1" out_rec = in_rec + $CRLF cc = FILE_PUT(OutFile, out_rec) file_Close(OutFile) halt(0)
George
|
|