lod
New Member
I feel like a true programmer being a BASIC beginner
Posts: 16
|
on line
Oct 13, 2018 5:51:27 GMT -5
Post by lod on Oct 13, 2018 5:51:27 GMT -5
Hi,
I have a very simple request but I fail to find the good end of the rope :-)
I have 6 arrays on differents JS file. So in the html that means a <cript> tag for each one.
I thought it would be good to have them in on single JS but each array must be rewriten separately.
A kind of sequential file if I'm not mistaken.
How can I say to Liberty BASIC to write on a specific line with the open4output instruction ?
Open filepath$.js for output as #f
print #f (on line number ..), "array txt"
close #f
It would be easier than a sequential file which's not txt based.
Thanks
|
|
|
on line
Oct 13, 2018 8:30:53 GMT -5
Post by mknarr on Oct 13, 2018 8:30:53 GMT -5
That the beauty of random access files. Once the file is open you can write or read to any record which in your case would be a line number. People think RAF are complicated but they aren't. In your case it might look like.
open File$ for random as #1 len=How ever long you think the lines will be. field #1, Linelength as Textsrring$
Once you have written the lines to the file and the file is open, you simply use get #1, linenumber
or put #1, linenumber
Because your field statement defines what you are putting or getting, Textstring will be in the current line number.
|
|
|
on line
Oct 13, 2018 16:15:41 GMT -5
Post by tsh73 on Oct 13, 2018 16:15:41 GMT -5
You CAN NOT - *this way* 1)open4output clears file on the spot 2)even if you open file FOR BINARY, there is no "line" you can replace There is bytes you can overwrite - but no "insert bytes" or "remove bytes". So changing "Line 3" to "New Line 3", you spoil next line(s)
So I think right way is
read file line by line on specific line, write new content to new file on other lines, write these lines to new file
delete old file (BACK IT UP first!!!) rename new file to old name.
See the example.
'Task: replace line #3 in a file with new text.
fname$ = "sample.txt" gosub [create] gosub [read] print
'try to rewrite line #3 print "try to rewrite line #3" nLine = 3 newLine$ = "New Line 3"
open fname$ for binary as #3 'both input/output 'read 2 lines, line by line for i = 1 to nLine-1 'print "LOC( #3 ) ";LOC( #3) line input #3, aLine$ next 'rewrite next portion 'print "LOC( #3 ) ";LOC( #3) print #3, newLine$ 'print "LOC( #3 ) ";LOC( #3) 'leave rest as is close #3 print "Line ";nLine;" is changed to ";newLine$
'check gosub [read] print "*obviously BAD*" print "ALWAYS will fail if length of new line <> length of old one" print
'now, right way to do it print "now, right way to do it" gosub [create] 'read old file, write to new file. On necessary line, write new line. 'Later rename new file to old name '(old one essentially - deleted, MAKE A COPY FIRST !!!) newfname$ = "temp.txt"
open fname$ for input as #2 open newfname$ for output as #1 i = 0 while not (eof(#2)) i = i+1 line input #2, aLine$ if i = nLine then print #1, newLine$ else print #1, aLine$ end if wend close #1 close #2
'(old one essentially - deleted, MAKE A COPY FIRST !!!) KILL fname$ name newfname$ as fname$
'check gosub [read] print "*OK*"
end
[create] 'create file open fname$ for output as #1 for i =1 to 5 print #1, "Line ";i next close #1 print "File ";fname$;" (re) created" return
[read] 'retrieve file, line by line print "*reading*" open fname$ for input as #2 i = 0 while not (eof(#2)) i = i+1 line input #2, aLine$ print using("##",i);">";aLine$ wend close #2 print "File ";fname$;" read - ";i;" lines" return
|
|
lod
New Member
I feel like a true programmer being a BASIC beginner
Posts: 16
|
on line
Oct 14, 2018 2:23:16 GMT -5
Post by lod on Oct 14, 2018 2:23:16 GMT -5
The first response, the RAF is hardly feasible because the LEN should be dynamic (and may go over 1000000) and even if I found a way to affect this automatically , this huge number still needs to be accurately divided for each Field. RAF are very sensitive to this parameter. If the Fields don’t add up exactly to the LEN the file is dead.
The second reminds me how silly I am to not reckon on a real database, be it nosql. I’ll try it though. Thanks
|
|
|
on line
Oct 14, 2018 4:18:57 GMT -5
Post by Rod on Oct 14, 2018 4:18:57 GMT -5
To me the obvious answer is to keep six files. There are no restrictions, you get fast access and you can load and save without issue, Windows takes care of it.
|
|
lod
New Member
I feel like a true programmer being a BASIC beginner
Posts: 16
|
on line
Oct 14, 2018 23:55:37 GMT -5
Post by lod on Oct 14, 2018 23:55:37 GMT -5
I thought of an easiest solution which is to simply dump the files in a single file. with 6 open 4 output instructions. Sometimes...things burst into my brain. Then I read this post. Would it be longer to load one big js ?
|
|
|
on line
Oct 15, 2018 2:49:49 GMT -5
Post by Rod on Oct 15, 2018 2:49:49 GMT -5
The answer probably depends on what your process is. You can easily open the file for output first and save the first array. Then open the file for append and save the next five arrays. To get them back you will need to know the exact size of each saved segment. You can then open the file for input and retrieve the segments.
What you can’t do is open the file and rewrite lines on a random basis. So save all, retrieve all is feasible
You can either keep another file with the segment lengths or head up the text with the segment length. Say the first 10 characters hold the segment length, read that then read the segment and repeat.
|
|
|
on line
Oct 15, 2018 9:30:40 GMT -5
Post by tsh73 on Oct 15, 2018 9:30:40 GMT -5
Hello there original question have distilled to this on other basic forum: '--------------------------------------------- 'finds line lineNum in sequential file fileName$ 'replaces it with newLine$ 'if no such line: change nothing sub writeAtLine fileName$,lineNum,newLine$ CRLF$= chr$(13) + chr$(10) open fileName$ for input as #writeAtLine 'input, build a file in memory txt$="": i=0 while not(eof(#writeAtLine)) i=i+1 line input #writeAtLine, aLine$ if i = lineNum then aLine$=newLine$ 'change line needed txt$=txt$+aLine$+CRLF$ 'build a file in memory wend close #writeAtLine 'write file from memory, all at once open fileName$ for output as #writeAtLine print #writeAtLine, txt$; '(;) prevents extra CRLF close #writeAtLine end sub
EDIT >>have distilled to this .. and it keeps boiling - BASIC is fun!
|
|