I have a program that saves a variable to disk (CurrentDB). This allow a user to enter a name for a database. This creates folders and such. The variable is saved onto SD card. Restarting the program is fine. However, if I turn off the phone and restart, then the file is sometimes messed up. There won't be any text in the file and the filesize will be 0. Sometimes it will be OK though with text.
Here are the important code. The point at which it crashes is in the sub 'INIloadCurrentDB' on line 'ListCDB = File.ReadList(File.DirRootExternal, "/Saved/PDTK/CurrentDB.txt")'. So looking at the code is it as simple as an incorrect code format or is there something else going on? Do you have to close out the file somehow when you exit the program so it saves the info? (Forgive me, I'm not that good of a programmer)
Here are the important code. The point at which it crashes is in the sub 'INIloadCurrentDB' on line 'ListCDB = File.ReadList(File.DirRootExternal, "/Saved/PDTK/CurrentDB.txt")'. So looking at the code is it as simple as an incorrect code format or is there something else going on? Do you have to close out the file somehow when you exit the program so it saves the info? (Forgive me, I'm not that good of a programmer)
B4X:
Sub Activity_Resume
CurrentDB = "Default" 'start as default and then rename it
If File.Exists (File.DirRootExternal, "/Saved/PDTK/CurrentDB.txt") Then
INIloadCurrentDB 'SR
CreateFileFolders 'SR
CreateFilePaths 'SR
Else
FTsetup 'SR
CreateFileFolders 'SR
CreateFilePaths 'SR
INIsaveCurrentDB 'SR
End If
end sub
Sub INIsaveCurrentDB
'-----------------------------------------------
If CurrentDB = "" Then
CurrentDB = "Default"
End If
'-----------------------------------------------
'Save location to text file on SD card
Dim ListCDB As List
ListCDB.Initialize
ListCDB.Add(CurrentDB) '0
'---------------------------------------
'leave absolution Address
File.WriteList(File.DirRootExternal & "/Saved/PDTK/", "CurrentDB.txt", ListCDB)
'---------------------------------------
Label1121L.Text = "DB: " & CurrentDB
'-----------------------------------------------
End Sub
Sub INIloadCurrentDB
'---------------------------------------------
'Loads location from text file on SD card and puts into variable
If File.Exists (File.DirRootExternal, "/Saved/PDTK/CurrentDB.txt") Then
Dim ListCDB As List
'leave absolute address
ListCDB = File.ReadList(File.DirRootExternal, "/Saved/PDTK/CurrentDB.txt")
CurrentDB = ListCDB.Get(0)
Else
CurrentDB = "Default"
End If
'---------------------------------------
If CurrentDB = "" Then
CurrentDB = "Default"
End If
'---------------------------------------
Label1121L.Text = "DB: " & CurrentDB
'-----------------------------------------------
End Sub
Sub CreateFileFolders
'-----------------------------------------
If File.Exists(File.DirRootExternal, "/Saved") = False Then
File.MakeDir (File.DirRootExternal, "/Saved")
End If
'-------------------------------------------------------------------------
If File.Exists(File.DirRootExternal, "/Saved/PDTK") = False Then
File.MakeDir (File.DirRootExternal, "/Saved/PDTK")
End If
end sub
Sub CreateFilePaths
'-------------------------------------------------------------------------
'Settings (Txt)
fpCDB = "/Saved/PDTK/CurrentDB.txt"
end sub