Jump to content

_ReplaceStringInFile UTF 8 BUG


goldenix
 Share

Recommended Posts

Make New UTF-8 File with notepad first : make New Text document.txt open it & save as New Text document.txt with utf-8 encoding. & rename it to New Text document.au3 unless you do this you cant run the code belw!

In sample 1 we will create new UTF-8 file & write a line онлайн in it, in next example we do the same, but after this we try to replace our line with string <done>онлайн. And hire it screws all up. Just run the code & compare 2 files & you will understand everything.

Mybe _ReplaceStringInFileshould be rewritten to modify existing file & not to create new one.?

So this is a bug right?

aa same goes for : _FileWriteToLine

#include <File.au3>

$file1 = 'test1.txt'
$file2 = 'test2.txt'

  ;## Sample 1  
    $hFile = FileOpen($file1, 128+1) ; make UTF- File
        FileWriteLine($hFile, "онлайн")
    FileClose($hFile)   

  ;## Sample 2
    $hFile = FileOpen($file2, 128+1) ; make UTF- File
        FileWriteLine($hFile, "онлайн")
    FileClose($hFile)
    
    _ReplaceStringInFile($file2,'онлайн', '<done>онлайн')
Edited by goldenix
My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

Test 2 works for me

go to File -> Encoding and select UTF8

Nahh it never workd, nor it does now. but i think i fixed it. im not 100% sure tho. I altered #include <File.au3> _ReplaceStringInFile function line 633 from:

$hWriteHandle = FileOpen($szFileName, 2)

to:

$hWriteHandle = FileOpen($szFileName, 128+2)

but will it cause any errors? I mean I dont understand 100% how this UDF is written.

EDIT: OK This does not work it corrupts the string in other manner now. In the end I had to use this code, to replace a line in non ansi enoding:

;## Get line from file & replace it with <Completed>myline
;~  ------------------------------------------------------  

$links_temp = 'links.temp'

    $file = FileOpen($links_txt, 128+0)
        $lineread = filereadline($file,$a)  ; read string at line   nr $a
    FileClose($file)
    
          ;## make UTF8 file first
            $hFile_ = FileOpen($links_temp, 128+1) ; make temp UTF- File
            FileClose($hFile_)
                        
                $file = FileOpen($links_txt, 128+0)     ; open in read mode
                $file2 = FileOpen($links_temp, 128+1)   ; open in write mode
                

                    If $file = -1 Then
                        MsgBox(0, "Error", "Unable to open file: " & $file)
                        Exit
                    EndIf

                    ; Read in lines of text until the EOF is reached
                    While 1
                        $line = FileReadLine($file)
                        If @error = -1 Then ExitLoop
                        
                      ;## if its our line replace it    
                        If StringInStr($line,$lineread) Then                
                            FileWriteLine($file2, '<Completed>' & $lineread)
                        Else
                            FileWriteLine($file2, $line)
                        EndIf
                    Wend

                FileClose($file)    
                FileClose($file2)
                
                FileMove($links_temp ,$links_txt,1) ; overwrite old links.txt
Edited by goldenix
My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

  • 2 months later...

Hi

I got the some same problem with UTF8 files and ReplaceStringInFile

I figure the same solution about the code replacement

$hWriteHandle = FileOpen($szFileName, 128 + 2)

As i work only in UTF8 or ANSI, i choose to copy/paste the _ReplaceStringInFile function to create a new _ReplaceStringInUTF8File function and change the

$hWriteHandle = FileOpen($szFileName, 2)
To
$hWriteHandle = FileOpen($szFileName, 128 + 2)

After many test, i nerver seen it corrupt the file or the string.

P.

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...