Sign in to follow this  
Followers 0
Sodori

FileWrite does not work for Danish symbols

6 posts in this topic

#1 ·  Posted (edited)

Hi,

Trying on my own leisure to work with a XML file. And it contains danish words such as "Påhængsvogn". Now, FileReadLine works excellent, it returns exactly that in fact I copied it from console thanks to FileReadLine! But, if I use FileWrite, the letters "å" and "æ" returns into an odd E5 and E6 value (there are some more down the code but nothing I would hope to try dig up. Similar principle)... Is there any sort of reason for it? My code is as dirty as a dog playing in mud, but here is is:

#include <File.au3>
#include <array.au3>

Local $Path = "C:\Danska Regnr\"
Local $aArray[1001]
Local $LineNumbers = 100000
Local $SourceFile = FileOpen($Path & "ESStatistikListeModtag.xml")
Local $ExtractedFile = FileOpen($Path & "First " & $LineNumbers & " lines.xml", 10)

ConsoleWrite(FileReadLine($SourceFile, 7) & @LF)

For $i = 1 To $LineNumbers
   FileWrite($ExtractedFile, FileReadLine($SourceFile, $i))
   If $i <> $LineNumbers Then FileWrite($ExtractedFile, @LF)
Next
;~ FileWrite

I've done some hard cleaning, so I can't validate it's working. But it should since I do not think I've removed anything critical.

Edited by Sodori

Share this post


Link to post
Share on other sites



it is how you open the file for writing 

these chars translate to HEX E5 and E6

å 229 E5 345   æ 230 E6 346

 

The default mode when writing text is ANSI so need to open the write mode as unicode

  $FO_UTF8 (128) = Use Unicode UTF8 (with BOM) reading and writing mode. Reading does not override existing BOM. 

Try this:

$f = FileOpen($Path & "First " & $LineNumbers & " lines.xml",138)
1 person likes this

Share this post


Link to post
Share on other sites

Awesome! That did the trick!! It's kinda confusing, but still makes sense! xD Let's hope I can fix the rest now then. Thank you!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I too recently noticed that this function of mine that writes away a column of text from an array doesn't seem to properly display some special characters (for example an ellips) in the resulting .txt file.

Can this be solved by f.e. including a FileOpen( $_filePath, 256+2 ) somewhere while still keeping my _FileWriteFromArray ...?

I'm only overwriting this file, so what would be the best code?

Do I then also have to use FileClose just before EndFunc?

Func _flushToFile()

        Local $_filePath = @ScriptDir & "\recent.txt"
        
;~      FileOpen( $_filePath, 256+2 )                       ; ok to add UTF-8...?
        
        _FileWriteFromArray($_filePath, _ArrayExtract($aArray, Default, Default, 1, 1) )
        
;~      FileClose( $_filePath )                             ; necessary?

EndFunc

Thx :)

Edited by Guy_

Share this post


Link to post
Share on other sites

_FileWriteFromArray will take the handle, returned by FileOpen, as the first parameter, so yes you can use FileOpen to open the file(s) in Unicode mode and write to them with _FIleWriteToArray. You would also need the FileClose statement when you're done reading or writing to the file.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Thx BrewManNH  :)

Turns out it was ok after all (in the beta).

Out of habit I was checking the text file with Total Commander's Lister (F3).

Even though that says "UTF-8", my ellipsis was displayed as a black square.

In notepad everything looks ok without needing the FileOpen stuff (which could be thanks to the AutoIt beta's new UTF-8 features too).

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