Jump to content

FileWrite doesn't work with UTF-opened files


Plummet
 Share

Recommended Posts

Hello.

I figured one critical problem for my code. My task is to modify the contents of a UTF8-encoded file (replace certain substrings in the right lines). I can't open it with FileOpen($file, 2), since it will rewrite the file in ANSI. So I open it with

$hFile = FileOpen($sFile, 256)

UTF8, no BOM. Then I try to use FileWrite:

FileWrite($hFile, $someMysteriousText)

Then I close the file with FileClose, and ... the file contents remain unchanged. It'll be cahnged if I use the modes 1 or 2 though (write with append/write with erase).

How can I correctly write to UTF8 files?

By the way, there's another problem with those modifiers — it is impossible to erase-write into file, only to append-write. But this is solvable by a hack (opening in ANSI for erase-write, writing "", closing, opening for UTF8 write). If only UTF-write will work.

Edited by Plummet
Link to comment
Share on other sites

As the helpfile says,

0 = Read mode (default)

So to write "UTF8 (without BOM)" use 256+1 (or 257). If you use 2 instead of 1 all content will disappear. This is all in the helpfile if you look at FileOpen().

it is impossible to erase-write into file, only to append-write

Don't understand what you mean there.
Link to comment
Share on other sites

Hm, helpfile says „Can be a combination of the following:“, where 0 is for read, 1 is for write and append, 2 is for write and erase, 256 is to „Use Unicode UTF8 (without BOM) reading and writing mode“. I wasn't clearly understanding what the „combination” is in this context, but if it's simply a sum... thanks a lot! I'll go trying it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...