Page 1 of 1

Missing first char when reading from a unicode file

Posted: Fri Oct 19, 2007 5:14 am
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.

Posted: Fri Oct 19, 2007 10:48 am
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

Posted: Fri Oct 19, 2007 3:33 pm
by Mistrel
That's it! Thank you. :)