QB64 Phoenix Edition
Storing stuff - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10)
+---- Thread: Storing stuff (/showthread.php?tid=1274)

Pages: 1 2


RE: Storing stuff - SpriggsySpriggs - 12-15-2022

@NasaCow
I'd recommend Proton VPN. It's pretty good.


RE: Storing stuff - bplus - 12-15-2022

@NasaCow

Open file For Input is read only in sequential order from first line to last.

Open file for Output is write only and will over write the last file by same name again it writes in sequential order from first line to last.

Open file for Binary reads and writes with get and put in byte quantities specified in length of get or put variable value or type.

Open file for Random also reads and writes to any part of file but usually of one and same record length setup with UDT using fixed lengths for strings.

So I'd say Input / Output is easiest for beginner and you can get very fancy with Binary reading and writing anywhere with any amount of bytes. 

Data records for 60 students, I doubt there is any difference you could feel for any access method over another.

I like Input / Output access because no matter how much faster Binary is, using arrays for processing data is faster than using a file as an array.
So with Input / Output you load arrays at start of data session and save data to files at end when done.


RE: Storing stuff - bplus - 12-15-2022

That said, there is a really nice way to load and save line record files of variable length strings using Split and Join$ routines.

To load:
1. Open file for Binary
2. Read it's size and make a get buffer
buff$ = space$(lof(filenum))
3. Get fileNum,  , buff$
4. close
5. Split buff$, delimiter$, myArr$()

MyArr$ now contains all the variable length file lines in an array for processing data in code.

but now I have to explain delimiter$ which is Chr$(13) + Chr$(10) for bas and old txt file but some modern txt files now just use chr$(10) or you could just use Chr$(10) and still read it from WP. They separate one "record" or variable length string from another.

and MyArr$() needs to be dynamic ie started with REDIM not Dim for the particular Split sub I have in mind:
(Dynamic means it can be resized as needed with REDIM _Preserve or just REDIM to clear last array and start with new with same variable name.

Code: (Select All)
' note: I buggered this twice now, FOR base 1 array REDIM MyArray (1 to 1) AS ... the (1 to 1) is not same as (1) which was the Blunder!!!
'notes: REDIM the array(0) to be loaded before calling Split '<<<< IMPORTANT dynamic array and empty, can use any lbound though
'This SUB will take a given N delimited string, and delimiter$ and create an array of N+1 strings using the LBOUND of the given dynamic array to load.
'notes: the loadMeArray() needs to be dynamic string array and will not change the LBOUND of the array it is given.  rev 2019-08-27
Sub Split (SplitMeString As String, delim As String, loadMeArray() As String)
    Dim curpos As Long, arrpos As Long, LD As Long, dpos As Long 'fix use the Lbound the array already has
    curpos = 1: arrpos = LBound(loadMeArray): LD = Len(delim)
    dpos = InStr(curpos, SplitMeString, delim)
    Do Until dpos = 0
        loadMeArray(arrpos) = Mid$(SplitMeString, curpos, dpos - curpos)
        arrpos = arrpos + 1
        If arrpos > UBound(loadMeArray) Then ReDim _Preserve loadMeArray(LBound(loadMeArray) To UBound(loadMeArray) + 1000) As String
        curpos = dpos + LD
        dpos = InStr(curpos, SplitMeString, delim)
    Loop
    loadMeArray(arrpos) = Mid$(SplitMeString, curpos)
    ReDim _Preserve loadMeArray(LBound(loadMeArray) To arrpos) As String 'get the ubound correct
End Sub

Function Join$ (arr() As String, delimiter$)
    Dim i As Long, b$
    For i = LBound(arr) To UBound(arr)
        If i = LBound(arr) Then b$ = arr(LBound(arr)) Else b$ = b$ + delimiter$ + arr(i)
    Next
    Join$ = b$
End Function

Where there is splitting there is complementary Join$ that converts an array into a really long string. That's when you want to file a variable string array back in a single Put for binary or easiest Open for Output and print filenum, the Join$'d string.

Then to file data from MyArr$()
buff$ = Join$(MyArr$(), Chr$(13) + Chr$(10))

and of course use
delimiter$ = Chr$(13) + Chr$(10)
because you don't want to do that + for every line!

So save a variable string array to file:
Open file for output as #filenum
Print #filenum, Join$(myArr$(), delimiter$)
Close

But these methods aren't felt in speed until in 1000's of strings.

In summary, it's nice if you have Split and Join$ in your arsenal of tools and know how to use them and, you have a file with maybe 1000's of lines to load or save.