Saving and reading data (.ini file)

cla721

Member
Licensed User
Longtime User
Saving and reading data (.ini file) SOLVED

I want my program to read a datafile on startup and save a datafile when the program ends. In Visual Basic I use this:

Public Sub INIfilesave()
Close #1
Open (App.Path & "\HLPS.ini") For Output As #1
Write #1, clasoft
Write #1, maxtoweight
Write #1, maxldweight
Write #1, pallpos16
Close #1
End Sub


Public Sub INIfileopen()
Open (App.Path & "\HLPS.ini") For Input As #1
Input #1, clasoft
Input #1, maxtoweight
Input #1, maxldweight
Input #1, pallpos16
Close #1
End Sub

How do I do it in Basic4android?

Regards Claus
 
Last edited:

cla721

Member
Licensed User
Longtime User
Hi Klaus

I have tried File.WriteString - File.WriteList - File.WriteMap and TextWriter.

But it doesn't work, or make any sence to me.

I need an example.

Claus
 

Cableguy

Expert
Licensed User
Longtime User
Have a Go at this library, it should provide access to INI files is a very easy way
 

cla721

Member
Licensed User
Longtime User
Hi Guys

I have made this code. It dosn't work, but am I going the right way??

Regards

Claus


B4X:
Sub Process_Globals

End Sub

Sub Globals

   Dim Label1 As Label
   Dim Label2 As Label
   
   Dim clasoft As Int
   Dim maxtoweight  As Int
   Dim maxldweight  As Int
   Dim pallpos16  As Int
   
End Sub

Sub Activity_Create(FirstTime As Boolean)

Activity.LoadLayout("TEST")
  
    clasoft = 1
    maxtoweight = 2
    maxldweight = 3
    pallpos16 = 4
   
    WriteTextWriter
    ReadTextReader
   
End Sub

Sub WriteTextWriter

'This should write the programdata when the program ends.
       
   Dim TextWriter1 As TextWriter
    TextWriter1.Initialize(File.OpenOutput(File.DirRootExternal, "Text.txt", False))
    
        TextWriter1.WriteLine(clasoft)
   TextWriter1.WriteLine(maxtoweight)
   TextWriter1.WriteLine(maxldweight)
   TextWriter1.WriteLine(pallpos16)
      
    TextWriter1.Close
   
End Sub

Sub ReadTextReader

'This should read the saved programdata on startup.

    Dim TextReader1 As TextReader
   TextReader1.Initialize(File.OpenInput(File.DirRootExternal, "Text.txt"))
    Dim line As String
    line = TextReader1.ReadLine    
    
   Do While line <> Null
    Log(line) 
    line = TextReader1.ReadLine
      clasoft = TextReader1.ReadLine
      maxtoweight = TextReader1.ReadLine
      maxldweight =  TextReader1.ReadLine
      pallpos16 = TextReader1.ReadLine
   Loop 
   
   TextReader1.Close   
        
   label1.Text = clasoft & maxtoweight & maxldweight & pallpos16 
   
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
If you want to write each value in its own row then you should use File.WriteList / ReadList.

B4X:
   Dim list1 As List
   list1.Initialize
   list1.Add(clasoft)
   list1.Add(maxtoweight)
   ...
   File.WriteList(File.DirRootExternal, "Text.txt", list1)
   
   'reading
   Dim list2 As List
   list2 = File.ReadList(File.DirRootExternal, "Text.txt")
   clasoft = list2.Get(0)
   maxtoweight = list2.Get(1)
   ...

Note that using pairs of keys and values with File.WriteMap / ReadMap is a better solution in most cases as you can later add new keys without breaking old files.
 

cla721

Member
Licensed User
Longtime User
Hi Erel

THANKS, that was just what I needed :sign0098:

Best regards

Claus, Denmark
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…