B4J Question Problem with File.WriteString i File.ReadString

Zlgo

Member
Hi
I want to change some "variables" in file print.prn which is "form" for printing bills on serial pos printer.In this file is
"ESC"sequences and I change this varibales (xxxx and yyyy) with real data like xxxx change with number for barcode and yyyy for date and time.
This part I do for example with code "Datoteka= Datoteka.Replace("xxxx","88888")", but problem is that when I send to printer this file instead
of barcode stay "unknown barcod type". So after looking what is the problem ("variabla" xxxx is corrected replaced with real data) find that opening
(file.ReadString) and just create the new but the same file with File.WriteString not produce exactly same copy because occur change in sam ESC sequences
and printing is not correct. Is there other solution to do this replacement in file without unwanted change file, and in same time make replacement just targeted data?
 

Daestrum

Expert
Licensed User
Longtime User
Can you post an example of the file before the changes (just a small sample).
 
Upvote 0

Zlgo

Member
In attachment is file ispis_org.txt which is used as template for create same file with only changed this two "variables" with name ispis.txt (extension prn are not allowed to upload on forum). It looks that File.Write / Read bytes don't ruin file, but I have to figure out how to make necessary change on wanted strings without very practical "replace"metod which exists in File.WriteString?
 

Attachments

  • ispis.txt
    212 bytes · Views: 192
  • ispis_org.txt
    210 bytes · Views: 177
Upvote 0

Zlgo

Member
Believe that this is not the best solution for my problem ,but it works without "damage" file with special characters.
With this code contents of file is transformed to byte , then to string where make change existing string "variable" xxxx with
real data("5555555") and againt to byte. Finishing saving to new file "ispis.prn" with help File.WriteBytes. For this case is
also important that CharSet is ASCII , UTF8 make wrong coding ESC command for printer which is include in file.



B4X:
Sub Button1_Click
    Private putanja As String = File.DirApp 'just place where this file exists
    putanja= putanja.Replace("Objects","Files")  
    Dim st As String
    Dim by() As Byte= File.ReadBytes(putanja,"ispis.prn")
    
    st=BytesToString(by,0,by.Length,"ASCII") 
    
    st=st.Replace("xxxx",555555)
    st=st.Replace("yyyy",DateTime.Now)
    
    by= st.GetBytes("ASCII")
    
    File.WriteBytes(putanja,"ispis.bit",by)
        
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code is safer as it doesn't convert the non-string bytes to a string:
B4X:
Sub AppStart (Args() As String)
    Dim bb As B4XBytesBuilder
    bb.Initialize
    bb.Append(File.ReadBytes(File.DirAssets, "ispis.txt"))
    SingleReplace(bb, "xxxx".GetBytes("ascii"), "55555".GetBytes("ascii"))
    SingleReplace(bb, "yyyy".GetBytes("ascii"), ("" & DateTime.Now).GetBytes("ascii"))
    File.WriteBytes(File.DirApp, "1.dat", bb.ToArray)
End Sub


Private Sub SingleReplace(bb As B4XBytesBuilder, SearchFor() As Byte, ReplaceWith() As Byte)
    Dim i As Int = bb.IndexOf(SearchFor)
    If i > -1 Then
        bb.Remove(i, i + SearchFor.Length)
        bb.Insert(i, ReplaceWith)
    End If
End Sub

Depends on B4XCollections.
 
Upvote 0

Zlgo

Member
This code is safer as it doesn't convert the non-string bytes to a string:
B4X:
Sub AppStart (Args() As String)
    Dim bb As B4XBytesBuilder
    bb.Initialize
    bb.Append(File.ReadBytes(File.DirAssets, "ispis.txt"))
    SingleReplace(bb, "xxxx".GetBytes("ascii"), "55555".GetBytes("ascii"))
    SingleReplace(bb, "yyyy".GetBytes("ascii"), ("" & DateTime.Now).GetBytes("ascii"))
    File.WriteBytes(File.DirApp, "1.dat", bb.ToArray)
End Sub


Private Sub SingleReplace(bb As B4XBytesBuilder, SearchFor() As Byte, ReplaceWith() As Byte)
    Dim i As Int = bb.IndexOf(SearchFor)
    If i > -1 Then
        bb.Remove(i, i + SearchFor.Length)
        bb.Insert(i, ReplaceWith)
    End If
End Sub

Depends on B4XCollections.
Thanks for this example!!
 
Upvote 0
Top