Modify

Opened 11 years ago

Closed 10 years ago

#2901 closed Bug (Fixed)

Text file as a binary

Reported by: anonymous Owned by: Jon
Milestone: 3.3.13.21 Component: AutoIt
Version: 3.3.13.19 Severity: None
Keywords: Cc:

Description

Example

#include 'FileConstants.au3'

$h = FileOpen('test.txt', $FO_OVERWRITE)
FileWrite($h, 'abcdef')
FileClose($h)

test(0)
test($FO_BINARY)
test($FO_UTF16_LE)
test($FO_UTF16_BE)
test($FO_UTF8)
test($FO_UTF8_NOBOM)
test($FO_UTF8_FULL)

Func test($FO)
  $h = FileOpen('test.txt', $FO_READ + $FO)
  $data = FileRead($h)
  FileClose($h)
  ConsoleWrite($data & " : " & IsBinary($data) & @CRLF )
EndFunc

3.3.12.0 output

abcdef : 0
0x616263646566 : 1
??? : 0
??? : 0
abcdef : 0
abcdef : 0
abcdef : 0

3.3.13.19 beta output

abcdef : 0
0x616263646566 : 1
0x63646566 : 1
0x63646566 : 1
0x646566 : 1
0x616263646566 : 1
abcdef : 0

Why all $FO_UTF* modes (except $FO_UTF8_FULL) return binary data?


If change string

FileWrite($h, 'abcdef')

to

FileWrite($h, ' ')

then "Error allocating memory" message will be shown for

test($FO_UTF16_LE)
test($FO_UTF16_BE)
test($FO_UTF8)

Change History (6)

comment:2 Changed 10 years ago by Jon

Bumped for visibility.

comment:3 Changed 10 years ago by mLipok

This problem is not only in reading as Binary, there is also problem with encoding:

here is repro:

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

_Test()

Func _Test()
	Local $sFileFullPath = @ScriptDir & '\test_file_encoding.txt'

	; FileWrite with $FO_UTF8_NOBOM
	_FileWrite_StringContent($sFileFullPath, 'poniedziałek, wtorek, środa, czwartek, piątek, sobota, niedziela')

	; FileRead with FileGetEncoding()
	Local $sResult = _FileRead_StringContent($sFileFullPath)
	MsgBox(0, '$sResult', $sResult)

	; FileRead - simply with out : FileOpen + FileClose
	Local $sResult2 = FileRead($sFileFullPath)
	MsgBox(0, '$sResult2', $sResult2)

EndFunc   ;==>_Test

Func _FileWrite_StringContent($sFileFullPath, $sContent)
	Local $hFile = FileOpen($sFileFullPath, $FO_OVERWRITE + $FO_CREATEPATH + $FO_UTF8_NOBOM)
	FileWrite($hFile, $sContent)
	FileClose($hFile)
EndFunc   ;==>_FileWrite_StringContent

Func _FileRead_StringContent($sFileFullPath)
	ConsoleWrite(FileGetEncoding($sFileFullPath) & @CRLF)
	Local $hFile = FileOpen($sFileFullPath, $FO_READ + FileGetEncoding($sFileFullPath))
	Local $sContent = FileRead($hFile)
	FileClose($hFile)
	if IsBinary($sContent) then
		ConsoleWrite('IsBinary -- converting to string')
		$sContent = BinaryToString($sContent,1 )
	EndIf
	Return $sContent
EndFunc   ;==>_FileRead_StringContent

comment:4 Changed 10 years ago by Jon

Looking into the memory problem in the first post.

I don't see any issues with the second post, it looks to be writing in UTF8-NoBOM and correctly detecting that when reading...Or am I missing something?

comment:5 Changed 10 years ago by mLipok

; FileWrite with $FO_UTF8_NOBOM
Writes "poniedziałek, wtorek, środa, czwartek, piątek, sobota, niedziela"

; FileRead with FileGetEncoding()
Reads "poniedziałek, wtorek, środa, czwartek, piątek, sobota, niedziela"

; FileRead - simply with out : FileOpen + FileClose
Reads "poniedziałek, wtorek, środa, czwartek, piątek, sobota, niedziela"

The problem is where I use FileGetEncoding()

btw.
testing AutoIt3.exe
https://www.autoitscript.com/forum/topic/173747-utf8utf16-testing/

works fine

comment:6 Changed 10 years ago by Jon

  • Milestone set to 3.3.13.21
  • Owner set to Jon
  • Resolution set to Fixed
  • Status changed from new to closed

Fixed by revision [11344] in version: 3.3.13.21

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.