Hi
I assume the problem is that the file is saved as UTF-8 from Notepad without BOM (https://en.wikipedia.org/wiki/Byte_order_mark). When opening a file in PowerBuilder you can provide the encoding (Ansi is the default), however if PB thinks the encoding provided doesn't match with the encoding in the file, FileOpen returns -1.
So you can't open the UTG-8 encoded file in PB with EncodingUTF8! as long as there's no BOM. The simples way to solve your problem is saving the file in Notepad with Encoding "UTF-8 with BOM".
An other option is that you read the first 3 bytes of the file and check for the BOM. If it's missing you can add the BOM by adding 3 bytes with the BOM to the blob and save it to a new file that you can read.
Or you can try to scan through the text and try to convert the funny characters with Swedish characters.
Regards
Arthur
John I may have misunderstood your response so my apologies if I did but I think you mean "...If you know the flat file will ALWAYS be UTF-8 encoded with BOM, use that encoding in the FileOpen call." as that is the only way it will work, it fails otherwise, unless there is some additional coding I'm not aware of that makes it work.
Aaron, to add on to Athur's suggestion, should you go that route, the characters he is referring to required at the beginning of the file are hexadecimal "0xEF 0xBB 0xBF". You certainly could do so but it is some extra processing that you may prefer not to add to your code.
When I know I'm reading a file that is UTF-8 without BOM, what I have done is open the file as EncodingANSI! Then read the file into a blob variable, then convert it to a string using EncodingUTF8! This allows the accented characters to stay intact.
It would look like the following:
li_file_handle = FileOpen(ls_path + ls_file_name, StreamMode!, Read!, LockReadWrite!, Append!,
EncodingANSI!) //EncodingANSI! required for accents in a file that is UTF-8 WITHOUT BOM
li_chars_read = FileReadEx(li_file_handle,lb_blob)
ls_file = String(lb_blob, EncodingUTF8!)
I have tried a lot of different combinations of FileOpen, FileReadEx and String conversions and this is the only one that seems to work under this scenario...but maybe there are others.
HTH...regards,
Mark
Regards,
Mark