Ciao,
sto cercando di scrivere e leggere una matrice da un file di testo. Per leggerla ho usato:
PUBLIC SUB salva_Click()
DIM hFile AS File
DIM stringa2 AS String
DIM a1 AS Integer
DIM b1 AS Integer
FOR a1 = 1 TO 8
FOR b1 = 1 TO 8
stringa2 = stringa2 & matr[a1, b1] & "\n"
NEXT
NEXT
hFile = OPEN User.home &/ "testo.txt" FOR CREATE
hfile.save(User.home &/ "testo.txt", stringa2)
CLOSE #hFile
END
e per leggerla ho usato
PUBLIC SUB carica_Click()
DIM hFile AS File
DIM Stringa2 AS String
hFile = OPEN User.home &/ "testo.txt" FOR INPUT
stringa2 = hfile.Load(User.home &/ "testo.txt")
matr = Split(stringa2, "\n", "", TRUE)
CLOSE #hfile
END
Dove matr è la matrice.
Il problema è che al caricamento, è come se mi eliminasse le dimensioni.
Infatti, provando a fare il debug, quando uso lo split mi da come errore "Bad number of dimension"
Ho trovato il codice sempre qua nel forum e li è sembrato funzionare.
Grazie
Perchè i cicli for iniziano da 1? Prova a farli partire da 0 e farli arrivare a 7 invece che 8....
Gli faccio partire da 1 perché la matrice di 0 è vuota, comunque ho fatto da 0 a 7 e ho aggiunto 1
In più devi prelevarti i dati per la matrici con due cicli for così come gli hai inseriti..
fatto, ma non va lo stesso
PUBLIC SUB carica_Click()
DIM hFile AS File
DIM Stringa2 AS String
DIM a1 AS Integer
DIM b1 AS Integer
hFile = OPEN User.home &/ "testo.txt" FOR INPUT
FOR a1 = 0 TO 7
FOR b1 = 0 TO 7
stringa2 = hfile.Load(User.home &/ "testo.txt")
NEXT
NEXT
matr = Split(stringa2, "\n", "", TRUE)
CLOSE #hfile
END
sempre nello split si ferma
FOR a1 = 0 TO 7
FOR b1 = 0 TO 7
dal codice si direbbe che le le dimensioni sono 8 :P almeno per gambas
Risolto: ecco il codice:
PUBLIC SUB carica_Click()
DIM hFile AS File
DIM a1 AS Integer
DIM b1 AS Integer
DIM sOneLine AS String
hFile = OPEN User.home &/ "testo.txt" FOR INPUT
a1 = 1
b1 = 1
WHILE NOT Eof(hFile)
LINE INPUT #hFile, sOneLine
matr[a1, b1] = sOneLine
a1 = a1 + 1
IF a1 = 9 THEN
a1 = 1
b1 = b1 + 1
ENDIF
IF b1 = 9 THEN
b1 = 1
ENDIF
WEND
CLOSE #hfile
END
N.B. la matrice parte da 1 e finisce a 8, in pratica ho letto linea per linea