Post by tsh73 on Oct 14, 2018 21:26:23 GMT
Creative misuse of file operations.
(inspired by
libertybasiccom.proboards.com/thread/322/on-line
)
Question:
Could we rewrite given line in text file?
Yes, right way:
Now,
Could we do it without creating copy of a file?
- well, with restrictions
(inspired by
libertybasiccom.proboards.com/thread/322/on-line
)
Question:
Could we rewrite given line in text file?
Yes, right way:
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 (may be BACK IT UP first)
rename new file to old name.
Now,
Could we do it without creating copy of a file?
- well, with restrictions
'tsh73 Oct 2018
'Task: replace line #3 in a file with new text.
'Attempt to do without rewriting whole file
'(OK of new text is shorter or could be truncated)
PRINT "creating data file"
fname$ = "sample.txt"
gosub [create]
gosub [read]
print
nLine=3
newLine$="Yep!"
print "try to rewrite line #";nLine
print "with a short line '";newLine$;"'"
call writeAtLine fname$,nLine,newLine$
'check
gosub [read]
print "=>Short line is padded with spaces"
print
nLine=3
newLine$="Old mcDonald had a ..."
print "with a long line '";newLine$;"'"
call writeAtLine fname$,nLine,newLine$
'check
gosub [read]
print "=>Long line is truncated"
print
nLine=10
newLine$="Not going to be written anyway"
print "try to rewrite non-existing line ";nLine
print "with a long line '";newLine$;"'"
call writeAtLine fname$,nLine,newLine$
'check
gosub [read]
print "=>File not changed"
end
[create]
'create file
open fname$ for output as #1
for i =1 to 5
print #1, "Line ";space$(i);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
'---------------------------------------------
'finds line lineNum in sequential file fileName$
'replaces it with newLine$ *inplace*
' pads it with spaces
' or truncates it so not to spoil next lines
'if no such line: do nothing
sub writeAtLine fileName$,lineNum,newLine$
open fileName$ for binary as #writeAtLine 'both input/output
'skip (lineNum-1) lines
for i = 1 to lineNum-1
if eof(#writeAtLine) then [writeAtLine.Exit]
line input #writeAtLine, aLine$
next
startPos=LOC(#writeAtLine)
if eof(#writeAtLine) then [writeAtLine.Exit]
line input #writeAtLine, aLine$
aLen=len(aLine$)
'rewind to start of the line and rewrite it
seek #writeAtLine, startPos
print #writeAtLine, padr$(newLine$,aLen)
[writeAtLine.Exit]
close #writeAtLine
end sub
'---------------------------------------------
'adds spaces from the left until 'n' symbols
'if n<len(a$) returns left$(a$,n)
function padl$(a$,n)
padl$ = left$(space$(n-len(a$))+a$,n)
end function
'adds spaces from the right until 'n' symbols
'if n<len(a$) returns left$(a$,n)
function padr$(a$,n)
padr$ = left$(a$+space$(n-len(a$)),n)
end function