Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Indexed files
#11
Use ```SPACE$(1)``` instead of ```CHR$(0)```. What if you need to port the program to Freebasic or other BASIC that can't handle ASCII0 as well? Yes I know it's a placeholder but ```SPACE$()``` was created especially for allocating a fixed number of characters into a variable-length string. Alternatively ```Z$``` could have been declared as ```STRING * 1``` with the same result.
Reply
#12
Thanks to those who tried to help me, but...  Huh
I guess I'm too old or too dense, or both, to make sense of any of the examples.
I can't get my head around the Type structures etc.
So I guess I'll go back to my (very primitive) original plan (crawls back into cave and pulls bush across doorway).
The responses were very much appreciated.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#13
(11-04-2023, 07:44 AM)PhilOfPerth Wrote: Thanks to those who tried to help me, but...  Huh
I guess I'm too old or too dense, or both, to make sense of any of the examples.
I can't get my head around the Type structures etc.
So I guess I'll go back to my (very primitive) original plan (crawls back into cave and pulls bush across doorway).
The responses were very much appreciated.

You know I was thinking after my posts there is a really easy way to do this:
Just load the file into an array and then you have an indexed access!
When done processing whatever with array just load it back into a newly rewritten file.

Then you have indexed access without any fixing up of your file as long as 1 file line is 1 record.

The array will have to be dynamic which means you can change the size of the array ie for adding new records = lines
So don't say DIM MyArray say REDIM MyArray that sets up an array to be dynamic.
THEN you can use REDIM _PRESERVE to make the array bigger to add more records.
Just keep track total Records or lines = UBOUND of array.

Also use base 1 array so REDIM MyArray(1 to MaxRecords) so the Ubound of array IS the number of records you have, no fiddling with +-1 like with 0 based arrays.

Piece of Cake man! Smile
b = b + ...
Reply
#14
(11-04-2023, 07:44 AM)PhilOfPerth Wrote: Thanks to those who tried to help me, but...  Huh
I guess I'm too old or too dense, or both, to make sense of any of the examples.
I can't get my head around the Type structures etc.
So I guess I'll go back to my (very primitive) original plan (crawls back into cave and pulls bush across doorway).
The responses were very much appreciated.

Give this a shot -- it's as simple as they come, with no Types or anything fancy.   Just a file, a string, and some minor GET/PUT statements at work:

Code: (Select All)

CONST Length_of_Data = 30 'set a constant length for your record size

OPEN "temp.txt" FOR RANDOM AS #1 LEN = Length_of_Data 'Set that length for the file you're working with

DIM My_Junk AS STRING * Length_of_Data 'And create a string to hold your data the same size.

'Now, let's PUT some data into that file

My_Junk = "Hello World. #1 Record."
PUT #1, 1, My_Junk
My_Junk = "My name is Steve. #2 Record."
PUT #1, 2, My_Junk
My_Junk = "This is a demo! #3 Record."
PUT #1, 3, My_Junk
My_Junk = "Hiyas Phil! #4 Record."
PUT #1, 4, My_Junk
My_Junk = "I need coffee! #5 Record."
PUT #1, 5, My_Junk
My_Junk = "I'm finished here. #6 Record."
PUT #1, 6, My_Junk

'Did you notice where we use the record number as the 2nd element in that PUT statement?

'Now, let's get that random data back and print it ot the screen.
'First, in order from start to finish...
FOR i = 1 TO 6
GET #1, i, My_Junk 'Notice that we GET the record via that 2nd parameter as well
PRINT i, My_Junk
NEXT
PRINT
PRINT
DO
INPUT "Now, which record would you like to pull out randomly? =>"; record
IF record = 0 OR record > 6 THEN END
GET #1, record, My_Junk
PRINT "Record #"; record; ":"; My_Junk
LOOP

'And that's all there is to it!
Reply
#15
Hi Phil
Back on the old site I had a similar discussion with Steve on a project I was trying to get off the ground. I recall the topic we were using was called "Random Access to a Sequential File". The major stumbling block was finding a particular record in a sequential file. The first thing I remember Steve pointing out is that it can't be done accurately without knowing the length of each record. It's easy to determine the size of each variable within a record (ie a Long = 4 byte and an Integer = 2 etc). The challenge is the string variables. Back then Steve pointed out that the safest was to determine what the words were in the longest string and to be sure to pad with spaces those string words which were less than the longest.

If you have another way to calculate the exact length of each string's length, then you wouldn't need any spacing. 

It is a simple matter then of opening the file once and use Seek #1 (or Seek #100 whatever the file number you have chosen to open) to position the search at the beginning of the sequential file, Record 1 would be record 1 (ie Seek #1, 1). If memory services me right, the pointer finding the records will always end on the last data item so to get to the next record you have to add one (assuming you want the next record)

The way the random access worked, and I'm struggling with my memory here - say you started with search for record 22, then it was Seek #1,(record length *21+1). and the next search was for record 14, you would reset the search back to the beginning and do the same thing (Seek #1,1 to get back to the beginning) then Seek #1,(record length *14+1)

Sorry Phil if this is not what you were thinking of, and of course if I'm way out to lunch on the structural layout of the coding.
Reply
#16
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:
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 + ...
Reply
#17
An example of a random access database can be found here (Hello, Pete!  Tongue ):

Select Cases & Database
Reply
#18
Thanks again to all. You got me back out of my cave!
@bplus:  I've gone through your Line Editor, line by line, and commented an explanation of what you did, and finally got it!
It has functions in it that I probably won't need (Saving and Backup), but it does what I wanted - I can search for and read 
any line of my file, randomly.
Thanks again.  Big Grin
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#19
(11-05-2023, 12:21 AM)PhilOfPerth Wrote: Thanks again to all. You got me back out of my cave!
@bplus:  I've gone through your Line Editor, line by line, and commented an explanation of what you did, and finally got it!
It has functions in it that I probably won't need (Saving and Backup), but it does what I wanted - I can search for and read 
any line of my file, randomly.
Thanks again.  Big Grin

If you just want a reader then yes, no need to save or backup. But if you want to add and edit lines you want to save your work and back up your data from old file first just in case something happens to new data file.

But again talking simplest ways to go about all this is just use a Word Processor, maybe number the lines yourself but mine has line numbers like QB64 IDE. That way you can view more than one line at a time.

You could also load a text file into an array to sort the file by lines, say if all the lines start with a date. With my little line editor that would not work because lines start with numbers but using a WP and starting all lines with a Date: Year-MM-DD format you can order all your entries like I do with my accounting accounts transactions.
b = b + ...
Reply
#20
(11-05-2023, 01:55 PM)bplus Wrote:
(11-05-2023, 12:21 AM)PhilOfPerth Wrote: Thanks again to all. You got me back out of my cave!
@bplus:  I've gone through your Line Editor, line by line, and commented an explanation of what you did, and finally got it!
It has functions in it that I probably won't need (Saving and Backup), but it does what I wanted - I can search for and read 
any line of my file, randomly.
Thanks again.  Big Grin

If you just want a reader then yes, no need to save or backup. But if you want to add and edit lines you want to save your work and back up your data from old file first just in case something happens to new data file.

But again talking simplest ways to go about all this is just use a Word Processor, maybe number the lines yourself but mine has line numbers like QB64 IDE. That way you can view more than one line at a time.

You could also load a text file into an array to sort the file by lines, say if all the lines start with a date. With my little line editor that would not work because lines start with numbers but using a WP and starting all lines with a Date: Year-MM-DD format you can order all your entries like I do with my accounting accounts transactions.

Thanks bplus; got it. Smile
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply




Users browsing this thread: 1 Guest(s)