B4R Question Edit text file

Mostez

Well-Known Member
Licensed User
Longtime User
Hello,
I have 2K bytes text file(Zebra label printer code) i want to edit certain fields in this file before sending it to printer via serial connection i.e. item price or company name. is there a method to seek text within file and replace it, as loading whole file into array is memory consuming. ZPL file looks like this:

^FT160,128^XG000.GRF,1,1^FS
^FT20,87^A0N,28,28^FH\^FDPrd. ID^FS
^FT21,46^A0N,28,28^FH\^FDCCCC^FS ' I wand to edit this line, CCCC = company name
^BY2,2,28^FT73,176^BEN,,Y,N
^FD0123458888881^FS
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can open the file with SD.OpenReadWrite.

Read the file one byte after another until you reach the end of line character. Now check whether the next 5 bytes (if there are 5 bytes) are equal to "^FT21". If they are then you need to read the line and write back the modified line.

You can get or set the stream position with SD.Position.
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
Hello,
in this code, i read file line by line and replace parameters as needed then send the modified line to printer directly instead of send it to file and then to printer again, the code is OK, but I had to increase the stack buffer size to 3000! because of program crash. I also noticed that when ZPL file is more than 2k program crash also, though file size is not a matter for 'for-next' loop! Is there a better method than 'if .. else-if' to save memory.

thanks

B4X:
For F = 1 To fz
        Dim read As Int =  SD.Stream.ReadBytes(tmp,0,1)
        If read = 0 Then
            Return ERR_FILE_EOF
        End If
        Select tmp(0)
            Case 0X0D,0x0A
                If BC.IndexOf(Buffer,"oooo".GetBytes) > 0 Then
                    parm = Str.ReplaceString(Buffer,"oooo".GetBytes,OwnerName) 'owner name field
                else if  BC.IndexOf(Buffer,"nnnn".GetBytes) > 0 Then
                    parm = Str.ReplaceString(Buffer,"nnnn".GetBytes,itn) 'item name field
                else if BC.IndexOf(Buffer,"wwww".GetBytes) > 0 Then
                    parm = Str.ReplaceString(Buffer,"wwww".GetBytes,weight.GetBytes) 'weight
                else If BC.IndexOf(Buffer, "pppp".GetBytes) > 0 Then
                    parm = Str.ReplaceString(Buffer, "pppp".GetBytes,ipr) ' item unit price
                else If BC.IndexOf(Buffer, "gggg".GetBytes) > 0 Then
                    parm = Str.ReplaceString(Buffer, "gggg".GetBytes,NumberFormat(grs,1,2)) 'gross amount
                else If BC.IndexOf(Buffer, "cccc".GetBytes) > 0 Then
                    LabelCounter = LabelCounter + 1
                    parm = Str.ReplaceString(Buffer, "cccc".GetBytes,NumberFormat(LabelCounter,1,0)) 'label counter
                else if BC.IndexOf(Buffer,"tttt".GetBytes) > 0 Then
                    parm= Str.ReplaceString(Buffer,"tttt".GetBytes,"16:25".GetBytes) 'time, DS1302 not implemnted yet
                else if BC.IndexOf(Buffer,"dddd".GetBytes) > 0 Then
                    parm= Str.ReplaceString(Buffer,"dddd".GetBytes,"06/06/17".GetBytes) 'date
                else if BC.IndexOf(Buffer, "88888888888".GetBytes) > 0 Then
                    parm = Str.ReplaceString(Buffer, "88888888888".GetBytes,ItemAndWeight) 'item and weight barcode
                else if BC.IndexOf(Buffer, "99999999999".GetBytes) > 0 Then
                    parm = Str.ReplaceString(Buffer, "99999999999".GetBytes,ItemAndPrice) 'item and price barcode
                Else
                    parm = (Buffer) 'not parametric line, send it to printer as it is
                End If
            LabelPrinter.Write(parm)
            'UploadArray(parm,LabelPrinter)
            'RAF.CurrentPosition = 0
            ArrayIndex = 0
            For x = 0 To Buffer.Length - 1
                Buffer(x) = 0
            Next
        Case Else
            'RAF.WriteByte(tmp(0),RAF.CurrentPosition)
            Buffer(ArrayIndex) = tmp(0)
            ArrayIndex = ArrayIndex + 1
        End Select
    Next

    SD.Close
    SetSD(False)
    Return ERR_NO_ERR
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
i moved inner code, it works ok thanks, that was not the only problem, this line too:
B4X:
 Select tmp(0)
            Case 0X0D,0x0A
it should be
B4X:
 Select tmp(0)
            Case 0x0A
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…