Android Question saved txt file with spaces and mode not clear.

fifiddu70

Well-Known Member
Licensed User
Longtime User
hello, i have this problem when saved my document in txt:
this is code for saving document:
B4X:
Dim creatavolo As TextWriter
    Log("check sub conteggia if  tavolo content the number: " & txttavoloarrivi.Text & " or is empty")
    creatavolo.Initialize(File.OpenOutput(File.DirRootExternal,  txttavoloarrivi.Text.trim &".txt",True))
    creatavolo.WriteLine(txtintestazione.Text & CRLF)
    creatavolo.WriteLine( txtLog.Text & CRLF)
    creatavolo.Close
log("Saved document" & txttavoloarrivi.text & ".txt")
in log i see: Saved document 7
7 is the number casually saved, but not saved format .txt
i see in log the spaces, this example:

Saved document 7



V0
.txt
whi not saved withouth spaces, this V0 ? is unknow for me.
 

Star-Dust

Expert
Licensed User
Longtime User
If you use WriteLine you do not need CRLF
 
Upvote 0

fifiddu70

Well-Known Member
Licensed User
Longtime User
my problem is this line:
B4X:
creatavolo.Initialize(File.OpenOutput(File.DirRootExternal,  txttavoloarrivi.Text.trim &".txt",True))
the .txt is not attached with txttavoloarrivi.text.trim
in log i see the txt document:
Saved document 7



V0
.txt

this is all logs when i sent this document:
B4X:
Dispositivo
COMANDA N.3 Tavolo:7
20/10/2019 ORE:16.28
Cameriere: Anonimo
              
=== PIZZE ===
1 : Pizza Zahira - Euro 8.00
                            
=== PIZZE MAXI TAV.7 ===
                            
                              
=== BEVANDE TAV.7 ===
                            
                              
=== MN8 TAV.7 ===
                            
 Note:
TOTALE EURO: 8.00
id 7
                    
                    
                    
                          
                    
                    
                    
V0
Sono sulla sub conteggia
verifico sulla sub conteggia se il tavolo contiene il numero:  7
                    
                    
                    
                          
                    
                    
                    
V0
 oppure è vuoto
Ho creato un tavolo in documento di testo con nome:  7
                    
                    
                    
                          
                    
                    
                    
V0
.txt
chiamo la sub conteggia su btntrasmettiwifi_click
Astream_NewData disconnesso true
btnconnetti_Click true
Connesso in WiFi 192.168.1.153 9100
 Client_Connected ha trasmesso a btntrasmettiwifi_Click
Connessione true
Connessione true
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
What appears in the log if you add this short Sub:
B4X:
Sub LogString(S As String)
    Log(S.Length & " [" & S & "]")
End Sub
and these three lines:
B4X:
LogString(txttavoloarrivi.Text)                                                                          'add this line
LogString(txttavoloarrivi.Text.Trim)                                                                     'and this line
LogString(txttavoloarrivi.Text.Trim & ".txt")                                                            'and this line

creatavolo.Initialize(File.OpenOutput(File.DirRootExternal,  txttavoloarrivi.Text.trim &".txt",True))    'before this existing line

reasoning being .Trim probably only removes spaces, and perhaps there are some other unexpected and/or non-visible characters in txtavoloarrivi.Text
 
Last edited:
Upvote 0

fifiddu70

Well-Known Member
Licensed User
Longtime User
emexes i tried to use your code, the lenght is 160, i have reduce this problem but i see this valor ( V0 )from number to .txt
i dot not.
this in log:
number: 7
V0
.txt
 
Upvote 0

emexes

Expert
Licensed User
emexes i tried to use your code, the lenght is 160, i have reduce this problem but i see this valor ( V0 )from number to .txt
i dot not.
this in log:
number: 7
V0
.txt
Then the problem is that txttavoloarrivi.Text is not just a simple number like you expect it to be.

What is txttavoloarrivi ? Is it a Label? An EditText? Show us the Dim of it.

Where does txttavoloarrivi get its value? Does the user type it in? Do you initialize it to something?

Without seeing the code that refers to txttavoloarrivi, it is difficult to say what is causing the problem.

Ideally, go to the File menu, do an Export To Zip, and post the Zip file to the forum using the forum "Upload a file".
 
Upvote 0

fifiddu70

Well-Known Member
Licensed User
Longtime User
Yes txttavoloarrivi.text is edittext used for receive via astream the value of sent via other phone, this is code:
B4X:
Sub Astreams_NewData(buffer() As Byte)
   
        If connected Then
        Dim msg As String
        Log("Astreams_NewData " & connected)
        msg = BytesToString(buffer,0,buffer.Length,"UTF8")
        txtLog.Text = txtLog.text & msg & CRLF
        txtLog.SelectionStart = txtLog.Text.length
        'ToastMessageShow(msg, False)
        Log(msg)
       
        Dim k2 As Int
        k2 = txtLog.Text.IndexOf("id")' I search for Table: to be able to extrapolate only the figure to be inserted in txtlog.text
        If k2 >= 0 Then
            txttavoloarrivi.Text = txtLog.Text.SubString2(k2 + 3, txtLog.Text.Length)'from here I extrapolate the table
            Log("the table in astream_Newdata e: " & txttavoloarrivi.Text)
            txtdisplay.Text=txtLog.Text
            Dim o As Int
            o = txtLog.Text.IndexOf("@")'I search for @:
            If o >=0 Then
                txtchiocciola.Text = txtLog.Text.SubString2(o -2 , txtLog.Text.Length)
                timer7.Enabled=True
               
            End If
           
            Dim s As Int
            s = txtLog.Text.IndexOf("#")' I search for @:
            If s >=0 Then
                txtcancelletto.Text = txtLog.Text.SubString2(s +1 , txtLog.Text.Length)
                timer7.Enabled=True
               
            End If
           
            mprx.Load(File.DirAssets,"rxmessage.mp3")
            mprx.Play
            timer6.Enabled=True
           
        End If
       
    End If
   
   
End Sub
Sub timer6_Tick
   
   If AStreams.outputqueuesize<> 0 Then Return
   timer6.Enabled=False
   AStreams.close
   Socket1.close
   Log( "Astream_NewData disconnesso " & connected)
   btnconnetti_Click
End Sub

Sub btnconnetti_Click
   If txtchiocciola.Text.Contains("@") Then
       Log("C'è la chiocciola")
   Else
       Log("btnconnetti_Click " & connected)
       If connected Then
       port = txtportaserver.Text
       ip  = txtipaserver.text
       CltSock.Initialize("Client")
       CltSock.Connect(txtipaserver.Text,txtportaserver.Text,20000)
       Log("Connesso in WiFi " & ip & " " & port & " " )
          
   
   
      
       Else
          
           Msgbox (" Not connected to a print ", "ALERT")
   End If
   
   End If
   
   
End Sub
 
Upvote 0

emexes

Expert
Licensed User
this is code
Four possibilities that jump out at me:

*** 1 ***

A complete message from the other phone might be split up into smaller chunks by the time it arrives at Astreams_NewData. It depends on the timing of the incoming bytes, versus the timing of Astreams checking for incoming bytes and delivering them to you via Astreams_NewData.

Worst case is you might receive each byte individually, one at a time, and the line "txtLog.Text = txtLog.text & msg & CRLF" will add a CRLF after each of those bytes.

Another scenario is that you might receive the word "id" split across two Astreams_NewData events, with the "i" at the end of one msg and the "d" at the start of the next msg, and your code will add a CRLF in-between the "i" and the "d", which means that the line "k2 = txtLog.Text.IndexOf("id")" will not find it.

*** 2 ***

I can see where you assemble the received chunks back into a single message with "txtLog.Text = txtLog.text & msg & CRLF"

but I cannot see where stuff is being removed from txtLog.Text after it has been processed,

thus it seems txtLog.Text would get longer and longer and longer.

*** 3 ***

This possibility is also a result of not receiving the entire packet all-at-once. What happens if you have received up to the word "id" but have not yet received everything after "id"?

Example: the other phone sent "314159 id=hello, world" but so far we have only received "314158 id=hell", and the rest of it "o, world" is still in the pipeline somewhere.

The line "txtLog.Text = txtLog.text & msg & CRLF" will add a CRLF to the end, so now we have "314159 id=hell" & CRLF
The line "k2 = txtLog.Text.IndexOf("id")" will find "id" at offset 7
The line "txttavoloarrivi.Text = txtLog.Text.SubString2(k2 + 3, txtLog.Text.Length)" will set txttavoloarrivi.Text to "hell" & CRLF

And then your earlier line "creatavolo.Initialize(File.OpenOutput(File.DirRootExternal, txttavoloarrivi.Text.trim &".txt",True))" will use that string, which contains a CRLF, to construct the filename "hell" & CRLF & ".txt"

I am guessing this is not what you want

*** 4 ***

The "-2" in the line "txtchiocciola.Text = txtLog.Text.SubString2(o -2 , txtLog.Text.Length)" makes me nervous...

What does .SubString do if variable o is 0 or 1, and thus offset "o -2" is -2 or -1?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…