WriteInt Help

rfresh

Well-Known Member
Licensed User
Longtime User
I'm trying to piece together various references on file i/o. I need to write an Integer to a file and have pieced together the following:

B4X:
   Dim raf As RandomAccessFile
   raf.Initialize(File.DirInternal, "1.dat", False)   
   WriteInt (Value As Int, Position As Long)
   close

I actually will need to write 3 integer values to this file so do I make 3 calls to WriteInt()?

But theRandomAccessFile is red in my code but I don't see a File IO lib to include.

Also I didn't see any Close examples and I'm not sure it's raf.Close or Close.raf?

In this case I want to write an integer value to the internal root section with the filename 1.dat

Thanks...
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can write strings to a random access file by using:
B4X:
Dim raf As RandomAccessFile
Dim b() As Byte
b = "sdfsdfsdfsdf".GetBytes("UTF8")
raf.WriteBytes(b, 0, b.Length, raf.CurrentPosition)

You will usually want to write the bytes length before writing the string. Otherwise you will not be able to know the string size when reading the file.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Gosh, that's somewhat complex.

How would that look to read it back into a variable?

Thanks...
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Mixing numbers and strings in a binary file is somewhat complex.
Why don't you use SQL database instead?
You can also use a text file and write each value in its own line.

You mean TextWriter and TextReader?
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
I need to save 3 Int data types, one Boolean and one string, so the text file would look something like this:

15
180
240
False
Indoor
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Here you are:
B4X:
Sub Globals
    Dim int1, int2, int3 As Int
    Dim bool1 As Boolean
    Dim str1 As String
End Sub

Sub Activity_Create(FirstTime As Boolean)

End Sub

Sub Activity_Resume
    If File.Exists(File.DirInternal, "test.txt") Then
        LoadFile
    Else
        int1 = 15
        int2 = 180
        int3 = 240
        bool1 = False
        str1 = "Indoor"
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    SaveFile
End Sub

Sub SaveFile
    Dim tw As TextWriter
    
    tw.Initialize(File.OpenOutput(File.DirInternal, "test.txt", False))
    tw.WriteLine(int1)
    tw.WriteLine(int2)
    tw.WriteLine(int3)
    tw.WriteLine(bool1)
    tw.WriteLine(str1)
    tw.Close
End Sub

Sub LoadFile
    Dim rw As TextReader
    
    rw.Initialize(File.OpenInput(File.DirInternal, "test.txt"))
    int1 = rw.ReadLine
    int2 = rw.ReadLine
    int3 = rw.ReadLine
    bool1 = rw.ReadLine
    str1 = rw.ReadLine
    rw.Close
End Sub
Best regards
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Writing double values to text file

Hello all, i'm trying to save the values read from an analog to digital converter to a text file, the values are actually a sine wave, but when i try to save the numbers to a text file, the values don't get saved each in their own line, i've tried writelist, and writemap but they both give me the same result, i'm reading the values from the analog to digital converter into an array(512), is there a way i can save the values so that each value takes its own line in the text file?

please help.

thanks,
Walter
 
Upvote 0
Top