Italian Errore su Lettura RandonAccessFile [SOLVED]

Star-Dust

Expert
Licensed User
Longtime User
A me funziona con il codice che ti allego

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Nome As String ="Nome"
    Dim Email As String = "[email protected]"
    Dim Pwd As String ="pwd"
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Type Utente(Codice As Int, User As String,Pwd As String, Email As String)
  
  
    Dim ListView2 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    ListView2.Initialize("ListView2")
    Activity.AddView(ListView2,0dip,0dip,50%x,100%y)
    
    ' Popola il file Users   
    For i=0 To 4
        Dim Rec As Utente
        Rec.User=Nome & " " & i
        Rec.Email=Email & " " & i
        Rec.Pwd=Pwd & " " & i
        WriteRec(Rec)
    Next
  
    ReadRec
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub BSignup_Click
    Dim Rec As Utente
    Rec.User=Nome
    Rec.Email=Email
    Rec.Pwd=Pwd
    WriteRec(Rec)
    'StartActivity(login)
End Sub


Sub WriteRec( rec As Utente )
    Dim f As RandomAccessFile
  
    f.Initialize(File.DirRootExternal,"Users", False)
    Dim RecNumber As Int = UltimoCodice
    rec.Codice=RecNumber
    Log("RECNUM" & RecNumber & " Codice " & rec.Codice & " user " & rec.User)
    f.WriteObject(rec, False, RecNumber)
    Log("rec " & rec & "RecNumber " & RecNumber)
    f.Close
End Sub

Sub UltimoCodice As Int
    Dim f As RandomAccessFile
  
    f.Initialize(File.DirRootExternal,"Users", False)
    Return f.Size
End Sub


Sub ReadRec
    Dim f As RandomAccessFile
    Dim Line As String
    Dim DataRecord As Utente

    f.Initialize(File.DirRootExternal, "Users" , True)
    Do While (f.CurrentPosition < f.Size )
    Log("Current " & f.CurrentPosition)
    DataRecord=f.ReadObject(f.CurrentPosition)
    Line=DataRecord.Codice & " " &  DataRecord.User & " " & DataRecord.Pwd & " " & DataRecord.Email
    Log(Line)
    ListView2.AddSingleLine(Line)
    Loop
f.Close
End Sub
 

Angelo Messina

Active Member
Licensed User
Longtime User
ok cosi funziona anche a me!

devo studiare qualche altra cosa per il salvataggio dell'indice

Grazie tanto
Sei stato gentilissimo
 

Star-Dust

Expert
Licensed User
Longtime User
Di niente.
ListaUser cosa è?
perché l'errore riguarda una matrice o vettore di dimenzione negativa -1
Non sarà ListaUser un vettore che va in errore?
 

Angelo Messina

Active Member
Licensed User
Longtime User
No l'errore è nel codice della sub UltimoCodice
infatti assegnava il numero di record tornando indietro
in poche parole non rispettavo la quantità di byte per ogni record

a parte la dimensione invertita della matrice

ok tutto a posto
 

Star-Dust

Expert
Licensed User
Longtime User
Scrivi [SOLVED] nel titolo del post.

Buona serata
 
Top