Missing first char when reading from a unicode file

Just starting out? Need help? Post your questions and find answers here.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Missing first char when reading from a unicode file

Post by Mistrel »

Code: Select all

OpenFile(1,"testfile.txt")
readstring.s=ReadString(1)
Debug Left(readstring.s,1)
readstring.s=ReadString(1)
Debug Left(readstring.s,1)
Create an ASCII and a UTF-8 file and put anything on the first two lines. The first character is not displayed in the debugger for the first line read from the unicode file but it is in the ASCII file.

Don't forget to turn unicode executable on in the compiler options!

Whatever it is it's really wrecking my code. It's being passed around as a blank string but it evaluates true against "" and " ". I even tried #CR$, #LF$ and #CRLF#. I don't know how to evaluate against it or even detect it.

I can even copy it from the debugger as "" in ascii but if I convert it to UTF-8 in Notepad++ it turns into an empty line.
Last edited by Mistrel on Sat Oct 20, 2007 7:48 am, edited 1 time in total.
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Post by nco2k »

dont forget to take the BOM into count:

Code: Select all

FileName.s = "C:\Test.txt"
If CreateFile(0, FileName)
  
  CompilerIf #PB_Compiler_Unicode
    StringFormat = #PB_UTF8
  CompilerElse
    StringFormat = #PB_Ascii
  CompilerEndIf
  
  WriteStringFormat(0, StringFormat)
  WriteString(0, "AString", StringFormat)
  WriteByte(0, 0)
  WriteString(0, "BString", StringFormat)
  WriteByte(0, 0)
  FlushFileBuffers(0)
  
  FileSeek(0, 0)
  StringFormat = ReadStringFormat(0)
  Debug "BOM size in bytes: "+Str(Loc(0))
  
  Debug ""
  
  AString.s = ReadString(0, StringFormat)
  Debug "AString: "+AString
  Debug "1st Char of AString: "+Left(AString, 1)
  
  Debug ""
  
  BString.s = ReadString(0, StringFormat)
  Debug "BString: "+BString
  Debug "1st Char of BString: "+Left(BString, 1)
  
  CloseFile(0)
  DeleteFile(FileName)
  
EndIf : End
c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

That's it! Thank you. :)
Post Reply