Modify

Opened 11 years ago

Closed 11 years ago

#603 closed Bug (Fixed)

_FileReadToArray() does not return an array if the file contains only a single line of text.

Reported by: Bowmore Owned by: Gary
Milestone: 3.2.13.9 Component: Standard UDFs
Version: 3.2.13.8 Severity: None
Keywords: Cc:

Description

In AutoIt 3.2.13.8 and previous beta versions _FileReadToArray() does not return an array if the file contains only a single line of text.
It does return an array in the Production version 3.2.12.1 of AutoIt

Test script to demonstrate behaviour in current beta

#include <file.au3>
#include <array.au3>
Global $g_sFileName = ''
Global $g_aFile = 0
Global $g_iStatus = 0
Global $g_sMessage = ''

;_FileReadToArray does not return array if file contains only 1 line of text
$g_sFileName = "C:\Data\TestFile_1.txt"
$g_iStatus = _FileReadToArray($g_sFileName, $g_aFile)
$g_sMessage = "@error       " & @TAB & " = " & @error & @CRLF
$g_sMessage &= "$g_sFileName" & @TAB & " = " & $g_sFileName & @CRLF
$g_sMessage &= "$g_aFile    " & @TAB & " = " & $g_aFile & @CRLF
$g_sMessage &= "$g_iStatus  " & @TAB & " = " & $g_iStatus
MsgBox(0, "Bug Test", $g_sMessage)
_ArrayDisplay($g_aFile)

;_FileReadToArray does returns array if file contains more than 1 line of text
$g_sFileName = "C:\Data\TestFile_2.txt"
$g_aFile = 0
$g_iStatus = _FileReadToArray($g_sFileName, $g_aFile)
$g_sMessage = "@error       " & @TAB & " = " & @error & @CRLF
$g_sMessage &= "$g_sFileName" & @TAB & " = " & $g_sFileName & @CRLF
$g_sMessage &= "$g_aFile    " & @TAB & " = " & $g_aFile & @CRLF
$g_sMessage &= "$g_iStatus  " & @TAB & " = " & $g_iStatus
MsgBox(0, "Bug Test", $g_sMessage)
_ArrayDisplay($g_aFile)

Attachments (2)

TestFile_1.txt (41 bytes) - added by Bowmore 11 years ago.
File with single line of text
TestFile_2.txt (71 bytes) - added by Bowmore 11 years ago.
File with two lines of text

Download all attachments as: .zip

Change History (4)

Changed 11 years ago by Bowmore

File with single line of text

Changed 11 years ago by Bowmore

File with two lines of text

comment:1 Changed 11 years ago by Bowmore

I have had another look at _FileReadToArray() and I now see what the problem is. With a single line file, which may or may not have a trailing new-line character, the function strips trailing whitespace before checking what type of new-line character is use. This results in no recognised new-line character being found in the file. This should be changed so that the type of new-line character is determined before the trailing whitespace is removed. Additionally I think it would be better if the whole file contents were returned in a single element of the array if the string could not be split

Suggested fix for this issue

Func _FileReadToArray($sFilePath, ByRef $aArray)
	Local $hFile = 0
	Local $aFile = ''
	Local $sSplitChr = ''
	
	$hFile = FileOpen($sFilePath, 0)
	If $hFile = -1 Then Return SetError(1, 0, 0);; unable to open the file
	;; Read the file
	$aFile = FileRead($hFile, FileGetSize($sFilePath))
	FileClose($hFile)

	If StringInStr($aFile, @LF) Then
		$sSplitChr = @LF
		StringStripCR($aFile)
	ElseIf StringInStr($aFile, @CR) Then ;;@LF does not exist so split on @CR
		$sSplitChr = @CR
	EndIf

	;; Remove any trailing white spaces
	$aFile = StringStripWS($aFile, 2)
	
	If StringLen($aFile) = 0 Then
		;; File is empty
		Return SetError(3, 0, 0)
	ElseIf $sSplitChr == '' Then
		;; unable to split the file
		;; store in single element
		Dim $aArray[2]
		$aArray[0] = 1
		$aArray[1] = $aFile
		Return SetError(2, 0, 0)
	Else
		$aArray = StringSplit($aFile, $sSplitChr)
	EndIf

	Return 1
	
EndFunc   ;==>_FileReadToArray

comment:2 Changed 11 years ago by Gary

  • Milestone set to 3.2.13.9
  • Resolution set to Fixed
  • Status changed from new to closed

Fixed in version: 3.2.13.9

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

Modify Ticket

Action
as closed The owner will remain Gary.
Author


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

 
Note: See TracTickets for help on using tickets.