In my topic the user "nonZero" posted this code. I haven't looked into it yet but I will be soon, maybe it's usefull also.
// Single-Array to text is pretty straightforward so I'll jump straight to a set of two dimensions
// Globals. Change destFile$ to wherever you like it.
GLOBAL destFile$: destFile$ = "C:\aaaTest.txt" // Can be constant
GLOBAL delim$: delim$ = CHR$(32) // Can be constant
GLOBAL dimCols: dimCols = 10
GLOBAL dimRows: dimRows = 20
GLOBAL maxDigits: maxDigits = 4 // Can be constant
DIM anArr(dimCols,dimRows)
// Make sure destFile$ doesn't exist (WARNING: Deletes if it does) and do setup stuff
SET WINDOW ON: SYNC RATE 60
IF FILE EXIST(destFile$) = 1: DELETE FILE destFile$: ENDIF
FOR a = 1 TO dimCols: FOR b = 1 TO dimRows: anArr(a, b) = RND((10 ^ maxDigits) - 1): NEXT b: NEXT a
// Okay, saving it easy enough, but retrieving is a problem. Since we are not using DBP's built-in commands,
// we need our own system. We therefore need to do one of two things: 1) either let the reading procedure know the dimensions
// beforehand or 2)save in a manner that makes it obvious. Because you want to manually edit these files, no. 2 would be
// best since it makes it human readable. This method is quite slow in DBP and there is quite a lot of mucking around involved
// because DBP is not just slow but limited in comparison to C.
// Saving is still straight-forward:`
tempStr$ = ""
OPEN TO WRITE 1, destFile$ // Open output file
FOR a = 1 TO dimRows // Iterate rows by columns
FOR b = 1 TO dimCols
tempStr$ = tempStr$ + PadStr$(anArr(a,b), maxDigits) // We pad it and add a delim for easy reading. We create 1 string per
// because DBP add a carraige return when using WRITE STRING
NEXT b
WRITE STRING 1, tempStr$ // Write the row
tempStr$ = "" // Clear the data so more columns can be added
NEXT a
WRITE STRING 1, "EOF" // Sadly the DBP file commands don't return values so this is easier
CLOSE FILE 1 // Close the file
PRINT "Done! Press a key..."
CLS
PrintOut(dimRows, dimCols, maxDigits) // Printout for comparison
WAIT KEY
EXECUTE FILE destFile$, "", "" // Open the resulting file for viewing
CLS
PRINT "I hope you didn't delete the file..."
PRINT "Press any key..."
WAIT KEY
DIM anArr(0,0) // Wipe the array out so we can test our load function
// Loading requires two things: 1) Determine the dimension and 2) Grab the data
CLS
PRINT "Reading from file..."
dimRows = 0: dimCols = 0 // Resetting these to ensure we'll get a good test
DIM tempRows$(1) // For storing our lines of columns
OPEN TO READ 1, destFile$ // Open input file
arrCount = 0 // Set array index counter to 0
REPEAT
INC arrCount // Increas array index counter by 1
DIM tempRows$(arrCount) // ReDim array - doesn't harm data. DBP is strictly $Dynamic
READ STRING 1, tempRows$(arrCount) // Get an entire line (automatically assumes 0D+0A to be teminator
UNTIL tempRows$(arrCount) = "EOF" // Our enforced EOF to make life easier
dimRows = arrCount - 1 // We now know how many rows (arrCount went up one last time so we clip it)
dimCols = 1 // Set dolumns to 1 to begin our dynamic array-sizing
FOR iii = 1 TO dimRows
DIM anArr(iii, dimCols) // ReDim
arrCount = 1 // It's 1 since we get the first token already
anArr(iii, arrCount) = VAL(FIRST TOKEN$(tempRows$(iii), " ")) // There's another method but since DBP added this function, why not use it
REPEAT
INC arrCount // Increas array index counter
IF arrCount > dimCols // ReDim array only if needed (or we will actually harm the data)
DIM anArr(iii, arrCount)
dimCols = arrCount
ENDIF
tempStr$ = NEXT TOKEN$(" ") // Sidenote: I actually use my own lib but, as I said, may as well use this.
anArr(iii, arrCount) = VAL(tempStr$) // Fill array with the value
UNTIL tempStr$ = ""
NEXT i
dimCols = arrCount - 1 // We need to remember arrCount was increased on the final run
DIM anArr(dimRows, dimCols) // We must clip that last dimension; a side effect of "the easy way"
PRINT "Finished reading!"
PRINT "Press any key for printout..."
CLS
PrintOut(dimRows, dimCols, maxDigits) // Printout for comparison
WAIT KEY
EXECUTE FILE destFile$, "", "" // Open the resulting file for viewing
WAIT KEY
END
Function PadStr$(inVal, maxLen)
ret$ = STR$(inVal)
tmpLen = maxLen - LEN(ret$)
FOR i = 1 TO tmpLen: ret$ = "0" + ret$: NEXT i // Std padding with preceding 0's so that number isn't harmed nor
ret$ = ret$ + delim$ // is delim affected when reading back.
EndFunction ret$
Function PrintOut(rows, cols, maxLen)
// Notes: The function will NOT print accurately because printing is wrapped and your columns may exceed your res
// The purpose of this is just to show that a) there is data and b) that certain numbers DO follow others in sequence
INC maxLen // This is just convenience math. We need a minimum of 1 space.
FOR a = 1 TO rows
FOR b = 1 TO cols
padding = maxLen - LEN(STR$(anArr(a,b)))
PRINT STR$(anArr(a, b)); // semi-colon denotes no carraige return after a PRINT
PRINT SPACE$(padding); // DBP's SPACE$() is quite a convenient function at times
NEXT b
PRINT "" // Easy Carraig Return insert
NEXT a
EndFunction