11-04-2023, 04:50 PM
Well the nice thing about Random Access is you deal directly with file so no worries about Saving after Open it but you do have to deal with fixed length lines.
So I made a very, very, very simple line editor for text files:
You enter a number not starting with 0 for line to see / edit
You enter a non number and rewrite the line
or you enter escape to quit and save the data to file.
I think I have all bugs out, let me know if you found one:
Come to think @PhilOfPerth why not just use an editor?
So I made a very, very, very simple line editor for text files:
You enter a number not starting with 0 for line to see / edit
You enter a non number and rewrite the line
or you enter escape to quit and save the data to file.
I think I have all bugs out, let me know if you found one:
Code: (Select All)
_Title "Random Access text file" ' bplus 2023-11-04
Dim Shared MyFile$ ' name of text/data file being a text file you can use any editor to edit
MyFile$ = "Test File Index Access.txt"
Dim Shared NRecs ' Number of records
NRecs = 1
ReDim Shared MyArr$(1 To NRecs)
Width _DesktopWidth * .9 / 8
LoadFile
Dim As Long edit, rec
edit = -1
rec = 1
While edit
Color 9
Print
Print "Rec Number:";
Color 15
Print rec
Color 9
Print " Rec line: ";
Color 15
Print MyArr$(rec)
Color 9
Print
Print "Enter number for record to read,"
Print "Don't start numbers with 0."
Print "enter non number to edit line,"
Print "enter esc to quit..."
Print
Color 15
Input u$
If u$ <> "" Then
If Asc(u$) = 27 Then
edit = 0: Exit While
ElseIf InStr("123456789", Left$(u$, 1)) Then
test = Val(u$)
If test > 0 Then
If test >= NRecs Then enlargeMyArr test
rec = test
Else
Beep
End If
ElseIf _Trim$(u$) <> "" Then
MyArr$(rec) = u$
End If
End If
Wend
SaveFile
End
Sub enlargeMyArr (recs)
If recs > UBound(MyArr$) Then ReDim _Preserve MyArr$(1 To recs): NRecs = recs
End Sub
' first thing to do for processing file data
Sub LoadFile ' loads file to working array with file lines as records
If _FileExists(MyFile$) Then
rec = 0
Open MyFile$ For Input As #1
While Not EOF(1)
Line Input #1, r$
rec = rec + 1
If rec > UBound(MyArr$) Then ReDim _Preserve MyArr$(1 To rec)
MyArr$(rec) = r$
Wend
Close #1
NRecs = rec
End If
End Sub
' last thing to do when done processing file data
Sub SaveFile ' saves myarr$ to file
' save a backup to MyFile$ in case something happens
If _FileExists(MyFile$) Then
Open MyFile$ For Input As #1
Open "MyOldFileBackup.txt" For Output As #2
While Not EOF(1)
Line Input #1, r$
Print #2, r$
Wend
Close
End If
Open MyFile$ For Output As #1
For i = 1 To NRecs
Print #1, MyArr$(i)
Next
Close #1
End Sub
Come to think @PhilOfPerth why not just use an editor?
b = b + ...