Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SEEK with INPUT for variable length strings
#1
Code: (Select All)
OPEN "data.txt" FOR OUTPUT AS #1
OPEN "data.ndx" FOR RANDOM AS #2 LEN = 4 'one long variable in size

DIM ndx AS LONG 'that long variable I mentioned above
DIM text AS STRING 'and a random length string
DIM count AS LONG 'and a counter for which element we want

DO
    ndx = LOF(1) + 1
    PUT #2, , ndx
    READ text$
    PRINT #1, text$
LOOP UNTIL text$ = "EOD"
CLOSE #1
OPEN "data.txt" FOR INPUT AS #1
'now we have a data file that we can use input with and read any record out of at will
DO
    INPUT "Which record would you like to retrieve =>"; count
    IF count = 0 THEN SYSTEM
    GET #2, count, ndx
    SEEK #1, ndx
    INPUT #1, text
    PRINT "That record was: "; text
LOOP

1

DATA one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve
DATA thirteen,fourteen,fiveteen,sixteen,seventeen,eighteen,nineteen,tenteen,eleventeen,twelveteen,EOD
Reply
#2
It should be noted this method works with 2 files, a file of strings usually a txt file and a file of indexes, ndx here, made with first code block but from Data. Maybe need a mod to "Random Access" any old txt file already existing? Try out the New OpenFileDialog for a txt file to index it?

This might be of practical use for seriously many lines of text, text files.

But if you edit the txt file you will have to re-index it, unless you just change one byte to another.
b = b + ...
Reply
#3
If you edit the text file, you just add the edited data to the end of the data.text and change the index to point to it.

For example, my data.txt file is: "onetwothreefour"

So my index file would be: 1,4,7,12

Now I want to change my second element from "two" to "apple juice".  Obviously, I can't put the 11 characters of "apple juice" in the three character spot of "two", so I just write them to the end of my data and change the index.

data.txt is now:  "onetwothreefourapple juice"

my index file is now: 1,16,7,12

There's 3 characters in the data.txt that are "lost" and not referenced anywhere, (that's the "two" that's still in there), but that may not be a big deal for me.  If it is, I'd probably want to write a routine to clean up and reindex the datafile while all my workers were at home and my company was shut down for the night.  (Say 2:00 AM Sunday morning for most folks.)  <-- That's called "Repacking" a database, and is generally just routine business which no one ever has to worry about except the data base programmer.  Wink

(And note, normally I'd either store my data with a terminator such as CRLF or CHR$(0), or else I'd have to store it with the length in my index as well.  I didn't do that here, as this is just talking about the concept and there's no actual code in this post to run or see the concept at work.  Wink )
Reply




Users browsing this thread: 1 Guest(s)