B4R Question SD-card: writing .CSV files

peacemaker

Expert
Licensed User
Longtime User
Hi, All

Just trying rSD lib with ESP32 and SPI card slot.
The main, it's usage "/" root prefix at the file names.

But how to save multiline text .CSV files correctly ?

Such sub...
B4X:
private Sub Savelog As Boolean
    If sd.OpenReadWrite("/log.dat") = True Then
        sd.Position = sd.CurrentFile.Size
        For i = 0 To 4
            Dim buff() As Byte
        
            buff = BC.StringToBytes(NumberFormat (i,10,0))
            sd.Stream.WriteBytes(buff,0,buff.Length)
        
            buff = BC.StringToBytes(";")
            sd.Stream.WriteBytes(buff,0,buff.Length)
        
            buff = BC.StringToBytes("Column_")
            sd.Stream.WriteBytes(buff,0,buff.Length)
          
            buff = BC.StringToBytes(NumberFormat(i, 2, 0))
            sd.Stream.WriteBytes(buff,0,buff.Length)
        
            buff = BC.StringToBytes(";")
            sd.Stream.WriteBytes(buff,0,buff.Length)
          
            buff = BC.StringToBytes(CRLF)
            sd.Stream.WriteBytes(buff,0,buff.Length)
       Next

        sd.Close
        Log("Saved to file OK")
        Return True
    Else
        Log("SD card writing error")
        Return False
    End If
End Sub

...gives this file content:

No idea why...
Any help ?
 

hatzisn

Expert
Licensed User
Longtime User
Try to set the buffer as fixed array and for each BC.StringToBytes create a new fixed array with length equal to string length, and BC.ArrayCopy it to buffer and write the buffer.
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
B4X:
Private Sub SaveToLog As Boolean
    Dim fn As String = "/log.dat"
    Dim i As ULong = 5
   
    Do While i > 0
        Dim a As String = JoinStrings(Array As String(NumberFormat (i,10,0), ";", "Column_", NumberFormat(i, 2, 0)))
        If Save_FileLine(fn, a) = False Then
            Log("SD card writing error")
            Return False
        End If
        i = i - 1
    Loop
    Log("Saved log file OK")
    Return True
End Sub

Public Sub Save_FileLine (filename As String, line As String) As Boolean
    If sd.OpenReadWrite(filename) = True Then
        sd.Position = sd.CurrentFile.Size
       
        Dim a As String = JoinStrings(Array As String(line, CRLF))
       
        Dim buff(a.Length) As Byte = Array As Byte(0)
        buff = BC.StringToBytes(a)
        sd.Stream.WriteBytes(buff,0,buff.Length)

        sd.Close
        Return True
    Else
        Return False
    End If
End Sub

It looks like the latest buffer is ... big and random:

What is wrong here ?
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Try with this:

B4X:
private Sub Savelog As Boolean
    If sd.OpenReadWrite("/log.dat") = True Then
        sd.Position = sd.CurrentFile.Size
        For i = 0 To 4
            Dim buff(10) As Byte
            
            Dim sI As String = NumberFormat (i,10,0)
            Dim b1(sI.GetBytes.Length) As Byte
            b1 = BC.StringToBytes(sI)
            BC.ArrayCopy(b1, buff)
            sd.Stream.WriteBytes(buff,0,buff.Length)
            
            Dim b2(";".GetBytes.Length) As Byte
            b2 = BC.StringToBytes(";")
            BC.ArrayCopy(b2, buff)
            sd.Stream.WriteBytes(buff,0,buff.Length)
        
            Dim b3("Column_".GetBytes.Length) As Byte
            b3 = BC.StringToBytes("Column_")
            BC.ArrayCopy(b3, buff)
            sd.Stream.WriteBytes(buff,0,buff.Length)
          
            Dim sC As String = NumberFormat(i, 2, 0)
            Dim b4(sC.GetBytes.Length) As Byte
            b4 = BC.StringToBytes(sC)
            BC.ArrayCopy(b4, buff)
            sd.Stream.WriteBytes(buff,0,buff.Length)
        
            BC.ArrayCopy(b2, buff)
            sd.Stream.WriteBytes(buff,0,buff.Length)
            
            Dim sCRLF As String = CRLF
            Dim b5(sCRLF.GetBytes.Length) As Byte
            b5 = BC.StringToBytes(sCRLF)
            BC.ArrayCopy(b5, buff)
            sd.Stream.WriteBytes(buff,0,buff.Length)
        Next

        sd.Close
        Log("Saved to file OK")
        Return True
    Else
        Log("SD card writing error")
        Return False
    End If
End Sub
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Try with this
Thanks, better, but anyway strange:

It seems to me, that here better to jump into the inline C++ https://www.b4x.com/android/forum/threads/rsd32-lib-and-rsd-lib-with-esp32.162629/, it works faster and without strange file content...
But as usual, impossible without puzzles: https://www.b4x.com/android/forum/threads/how-to-use-2-string-vars-in-inline-c.162641/
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…