Android Question File name with a space

Pacache

Member
Hello Everyone, i come back to you just like last year because i still have an unsolved issue with file names obviously containing a space or something else that i cant see with my eyes.
In a loop for next, i open many files but sometimes it says the file does not exist. Of course the file DOES exist but what is strange is that sometimes ( thought i change nothing) it works ok and sometimes it wont with the same file ! . I have now the problem with a file called "Genèse" when i call this file in the loop and log for its length, it mostly says 6 and suddenly it says 7 !!!!! and then 6 again ! how is it possible that in the same loop it can open the same file several times and suddenly the application seems to add a space or something else in its name ?
 

emexes

Expert
Licensed User
I have now the problem with a file called "Genèse" when i call this file in the loop and log for its length, it mostly says 6 and suddenly it says 7 !!!!! and then 6 again ! how is it possible that in the same loop it can open the same file several times and suddenly the application seems to add a space or something else in its name ?
Super! Reproducible bugs are great, even if not 100%.

Log the individual characters of the string too, to work out precisely what is being added.

My first thought is that, in UTF-8, the greater-than-ASCII character "è" would be two bytes rather than just one.

B4X:
Log(FullString("Hello world!!!"))
Log(FullString("Two bees, or not two bees?"))
Log(FullString("Genèse"))

Sub FullString(S As String) As String
  
    Dim sb As StringBuilder
    sb.Initialize
  
    Dim L As Int = S.Length
  
    sb.Append(L)
    sb.Append(" chars = (")

    Dim bc As ByteConverter
    Dim C() As Char = bc.ToChars(S)

    For I = 0 To L - 1
        If I <> 0 Then
            sb.Append(", ")
        End If
        sb.Append(Asc(C(I)))
    Next
  
    sb.Append(") = """ & S & """")
  
    Return sb.ToString
      
End Sub

Log Output:
Waiting for debugger to connect...
Program started.
14 chars = (72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33, 33, 33) = "Hello world!!!"
26 chars = (84, 119, 111, 32, 98, 101, 101, 115, 44, 32, 111, 114, 32, 110, 111, 116, 32, 116, 119, 111, 32, 98, 101, 101, 115, 63) = "Two bees, or not two bees?"
6 chars = (71, 101, 110, 232, 115, 101) = "Genèse"
 
Last edited:
Upvote 0

Pacache

Member
In the loop i ask to look into a big file. According to what he finds in this loop, he opens a file through a variable of course which can be "Genèse" or another one out of 65. This "Genèse" file can tho be opened several times in the SAME loop so sometimes the Genèse length is 6 and all of a sudden its 7!! i can bypass the issue easily, i just tell him if the name of the file contains("Gen")=true then the name of the file is "Genèse" and then it works. But i'm fed up doing that because every time i do that, another file will pop up with exactly the same mistake. the accent has nothing to do with it since i had to bypass the file name "Jean" and then another one named"Colossiens" and another one named "Marc" this is crazy !!. and you wont believe me sometimes i just erase what i call the bypasses and it works ok for all the files !!!! and the next day it wont !! its a mystery to me. ^^
 
Upvote 0

Pacache

Member
yes i though of trying to find out was was being added but didnt know how to do it. I will try your solution. Thank you very much !
 
Upvote 0

Pacache

Member
yes i though of trying to find out was was being added but didnt know how to do it. I will try your solution. Thank you very much !
Super! Reproducible bugs are great, even if not 100%.

Log the individual characters of the string too, to work out precisely what is being added.

My first thought is that, in UTF-8, the greater-than-ASCII character "è" would be two bytes rather than just one.

B4X:
Log(FullString("Hello world!!!"))
Log(FullString("Two bees, or not two bees?"))
Log(FullString("Genèse"))

Sub FullString(S As String) As String
 
    Dim sb As StringBuilder
    sb.Initialize
 
    Dim L As Int = S.Length
 
    sb.Append(L)
    sb.Append(" chars = (")

    Dim bc As ByteConverter
    Dim C() As Char = bc.ToChars(S)

    For I = 0 To L - 1
        If I <> 0 Then
            sb.Append(", ")
        End If
        sb.Append(Asc(C(I)))
    Next
 
    sb.Append(") = """ & S & """")
 
    Return sb.ToString
     
End Sub

Log Output:
Waiting for debugger to connect...
Program started.
14 chars = (72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33, 33, 33) = "Hello world!!!"
26 chars = (84, 119, 111, 32, 98, 101, 101, 115, 44, 32, 111, 114, 32, 110, 111, 116, 32, 116, 119, 111, 32, 98, 101, 101, 115, 63) = "Two bees, or not two bees?"
6 chars = (71, 101, 110, 232, 115, 101) = "Genèse"
 
Upvote 0

Pacache

Member
Thank you Emexes you were great help with you code. it helped me find out that the added char on 1st position was 65279. I dont know why this char is added from time to time for the same file but all i had to do is replace it by an empty character. 👍 I posted my code and the logs but were not issued, i probably did something wrong. Thanks a lot
 
Upvote 0

emexes

Expert
Licensed User
Thank you Emexes you were great help with you code. it helped me find out that the added char on 1st position was 65279. I dont know why this char is added from time to time for the same file but all i had to do is replace it by an empty character.
65279 = 0xFEFF = Unicode BOM (byte order mark)

which I vaguely remember is added by Windows Notepad (and presumably other editors) at the beginning of files saved as Unicode (or perhaps only 16-bit and 32-bit Unicode - UTF-8 is pretty clear on byte order).

https://en.wikipedia.org/wiki/Byte_order_mark

It would be better to delete the character rather than replace it by an empty(?) character:

B4X:
Dim CleanFileName As String = DirtyFileName.Replace(Chr(0xFEFF), "")

although maybe I have misunderstood, and your "empty character" means "empty string" ie no character.
 
Last edited:
Upvote 0
Top