Text file issue

IoSonoPiero

Active Member
Licensed User
Longtime User
[solved] Text file issue

Hello,
I'm in trouble with writing text files.

I have to get a file similar to "i_want_this.txt", but with the FileWrite function i get "i_have_this.txt"

What type of pagecode or other I have to use to get the "i_want_this.txt" file?

There is something to set? There is some library to use?

Someone can help me, please?

Thanks,
Piero
 

Attachments

  • i_have_this.txt
    19 bytes · Views: 220
  • i_want_this.txt
    30 bytes · Views: 194
Last edited:

maXim

Active Member
Licensed User
Longtime User
Hi pixelfabrica,

see the FilePut function in Main Help or WriteString in BinaryFile.

Best Regards,

Massimo
 

IoSonoPiero

Active Member
Licensed User
Longtime User
Hello Massimo,
I've used WriteString passing in constructor both UTF-8 and UTF-16 integer buth results are very strange, and doesn't give me the requested file.

I think (but this is my opinion) that the file "i_want_this.txt" is in Unicode (UTF-16), while "i_have_this.txt" is UTF-8.

Someone can confirm or negate my think?
 

IoSonoPiero

Active Member
Licensed User
Longtime User
I'll try again with Writestring.
I'll build a simple app that loop with all the code page available for the constructor to see if there is a way to accomplish this.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Do you want to encode it in UTF16?
If yes then you should use BinaryFile.New2 with code page 1200.
Then you should convert the string to bytes with BinaryFile.StringToBytes. The last step is to write the bytes with WriteBytes.
You should not use BinaryFile.WriteString as it adds the string length as a prefix.

Edit: Once again, I've wrote this post before reading your previous two...
 

IoSonoPiero

Active Member
Licensed User
Longtime User
I've tried and this is the result (attached file).

What do you think about that?

Is the carriage return (or LF) missing?
If I do a binary compare, I see differences at the beginning of file with the "i_want_this.txt" file.
 

IoSonoPiero

Active Member
Licensed User
Longtime User
I see two differences:
- utf16.txt is missing the UTF16 BOM. You should write the following two bytes at the beginning of the file: 255, 254.
- utf16.txt is missing the last chr(10) character. I think that it is LF but it doesn't really matter.
Ok, good.
So how can I add the two bytes at the beginning and the chr(10) at the end of the file?

With StringToBytes?
 

IoSonoPiero

Active Member
Licensed User
Longtime User
I've used this function

B4X:
FileOpen(c1,"temp.txt",cRandom)
    bf.New2(c1, 1200)
    
    ' Write BOM
    bf.WriteByte(255)
    bf.WriteByte(254)
    
    ' Scansiona ogni riga della tabella
    For i = 0 To (tbEditor.RowCount-1)
        ' Recupera la word
        strStringa = tbEditor.Cell("Word", i) & " "
        strStringa = strStringa & tbEditor.Cell("Priority", i)
        
        ' Create buffer
        buffer() = bf.StringToBytes(strStringa)
        bf.WriteByte(10)
        bf.WriteByte(0)
        
        ' Write buffer
        bf.WriteBytes(buffer())
    Next i


    FileClose(c1)

but the result file is different from the "i_want_you.txt"
Now they have the same size but a binary compare shows their differencies...
 

IoSonoPiero

Active Member
Licensed User
Longtime User
:signOops:

Yes, sure, sorry for this!

I've misplaced the instruction!

Now, it works!

Thank you very much, Erel!
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…