Sign in to follow this  
Followers 0
Plummet

FileWrite doesn't work with UTF-opened files

4 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Yeah, it's working. May be it'll be useful to define what is the combination explicitly in the docs ;) Thanks again.

Share this post


Link to post
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
Sign in to follow this  
Followers 0