Quote: "When I wrote my own piece of code it wouldn't work, when I copied the code from TDK's post it worked.. strange"
DBP is a little temperamental with file and data. It feels a little pointless me posting the examples I prepared since you got your text file working, but for reference (since I boiled them up anyway):
Both these focus on a human-readable type text file using different techniques and delimeters.
1. Using basic DBP alone. I've used low number for dimensions (10x20) because this method is slow. If you crank it to 99x99 it'll practically freeze.
// 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
2. VERY FAST: Using direct memory access & pointers. I've used 99x99 as apposed to 10x20 in this example. (Requires RPLib to be included. You can download it from the link in my sig if you're curious)
// This is a much faster example but complicated and requires my functions library (RPLib/LibRP)
// Globals. Change destFile$ to wherever you like it.
GLOBAL destFile$: destFile$ = "C:\aaaTest.txt"
GLOBAL dimCols: dimCols = 99
GLOBAL dimRows: dimRows = 99
GLOBAL maxDigits: maxDigits = 4
GLOBAL myFile: myFile = 1
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
// Creating a file that's human-readable
PRINT "Gonna make a file..."
OPEN TO WRITE myFile, destFile$
FOR a = 1 TO dimRows
FOR b = 1 TO dimCols
tmpPTR = rpdChar(STR$(anArr(a, b))) // Creates a char-alike stringbuffer from the string-cast of the vlue
tmpSize = rpdSizeOf(tmpPTR) - 1 // Stores the size of the above buffer for use in the FOR-loop
FOR sz = 0 TO tmpSize
tmpAddr = tmpPTR + sz // Add each iteration to the address to grab the next char
WRITE BYTE myFile, *tmpAddr // Write char at etc address to file
NEXT sz
rpdKill(tmpPTR) // Always remember to deallocate memory you make
WRITE BYTE myFile, 9 // Insert a tab as a delim
NEXT b
WRITE BYTE myFile, 13 // Place a carraige-return at the
WRITE BYTE myFile, 10 // end of each line
NEXT a
CLOSE FILE myFile
CLS
PrintOut(dimRows, dimCols, maxDigits)
PRINT "Done! Press a key"
WAIT KEY
EXECUTE FILE destFile$, "", ""
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 procedure
dimRows = 0: dimCols = 0 // Reset our dimensions to be fair
CLS
PRINT "Reading from file..."
// Reading back the file
curByte AS BYTE
scanPOS = 0
OPEN TO READ myFile, destFile$
MAKE MEMBLOCK FROM FILE myFile, myFile
CLOSE FILE myFile
maxPOS = GET MEMBLOCK SIZE(myFile)
mbPTR = GET MEMBLOCK PTR(myFile)
memPTR = MAKE MEMORY(maxPOS)
COPY MEMORY memPTR, mbPTR, maxPOS - 1
DELETE MEMBLOCK myFile
// Quickly fetch the rows and cols - this can be done simultaneously but it's easier and faster this way.
temp_maxPOS = maxPOS - 1
FOR i = 0 TO temp_maxPOS
scanAddr = memPTR + i
curByte = *scanAddr
IF curByte = 9: INC dimCols: ENDIF
IF curByte = 13: temp_maxPOS = i: ENDIF
NEXT i
temp_maxPOS = maxPOS - 1
FOR i = 0 TO temp_maxPOS
scanAddr = memPTR + i
curByte = *scanAddr
IF curByte = 13: INC dimRows: ENDIF
NEXT i
// Because we now know our array shape, let's fill it in as a form
DIM anArr(dimRows + 1, dimCols + 1) // Because array(0) is seldom used in BASIC, we need to begin at 1
// the extra dimensions (which will be trimmed) allow for iterations to reference
// those out of bounds.
cols = 1: rows = 1 // These'll be our counters
tempStr$ = "" // Reset temp string
temp_maxPOS = maxPOS - 1 // We use an offset as we iterate from 0
FOR i = 0 TO maxPOS
scanAddr = memPTR + i // Essentially we are adding iteration to the original point in memory
curByte = *scanAddr // Now we read the byte at the new address
IF curByte > 47 AND curByte < 58 // We can omit not a numeral (change condition if needed for alpha/alpha-numeric values)
tempStr$ = tempStr$ + CHR$(curByte)
ELSE // ELSE: IF it was NOT a numeral, check for special characters 9, 10, 13
IF curByte <> 10 // This part's sloppy but I'm tired and it's getting late: If not 10 (carraige return is 13+10 but we are using 13 as a flag)
anArr(rows, cols) = VAL(tempStr$) // then assign value-cast of string to current index at rows, cols
tempStr$ = "" // and cleanup string
IF curByte = 13
INC rows // Carraige causes a rows increment which means
cols = 1 // cols must be reset
ENDIF
IF curByte = 9
INC cols // Tab causes a cols increase
ENDIF
ENDIF
ENDIF
NEXT i
DIM anArr(dimRows, dimCols) // Trim the array back down
PrintOut(dimRows, dimCols, maxDigits)
EXECUTE FILE destFile$, "", ""
WAIT KEY
END
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
Hope there aren't too many typos and clumsy bits.
Good luck with the game and glad things came right.