Modify

Opened 18 years ago

Closed 18 years ago

#42 closed Bug (Fixed)

_FileWriteFromArray outputs too many CRLF (File.au3)

Reported by: mlowery <matin@…> Owned by: Gary
Milestone: 3.2.11.0 Component: Standard UDFs
Version: 3.2.10.0 Severity:
Keywords: Cc:

Description

Lines output from the _FileWriteFromArray function in the standard File.au3 include are prefixed by a @CRLF, resulting in an initial blank line in the file. Repeated use of this function and its complement _FileReadToArray on the same file can result in file bloating.

A suggested fix is to change the current file writing loop:

 	Local $ErrorSav = 0
 	For $x = $i_Base To $i_UBound
 		If FileWrite($hFile, @CRLF & $a_Array[$x]) = 0 Then
			$ErrorSav = 3
			ExitLoop
		EndIf
	Next

With something such as below that outputs the CRLF after the data, and does not write a final CRLF (code could undoubtedly be improved):

	Local $ErrorSav = 0
 	For $x = $i_Base To $i_UBound -1
		If FileWriteLine($hFile, $a_Array[$x]) = 0 Then
			$ErrorSav = 3
			ExitLoop
		EndIf
	Next
	If FileWrite($hFile, $a_Array[$i_UBound]) = 0 Then $ErrorSav = 3

As an additional suggestion, modify the function call to take the array by reference:

_FileWriteFromArray($File, ByRef $a_Array, $i_Base = 0, $i_UBound = 0)

Thanks!

Attachments (0)

Change History (4)

comment:1 by Gary, 18 years ago

Milestone: 3.2.11.0
Resolution: fixed
Status: newclosed

Already Fixed in 3.2.11.0

http://www.autoitscript.com/forum/index.php?showtopic=58503

	; Write array data to file
	Local $ErrorSav = 0
	For $x = $i_Base To $i_UBound
		If FileWrite($hFile, $a_Array[$x] &  @CRLF) = 0 Then
			$ErrorSav = 3
			ExitLoop
	EndIf
	Next

comment:2 by mlowery, 18 years ago

Resolution: fixed
Status: closedreopened

Thanks for the link; I did a forum search but was looking for the function name. :(

The fix above corrects the order of data and CRLF on a line, but the file will still grow if read/written multiple times.

Here's an example that reads and writes an array from a file multiple times. Blank lines are added at the beginning or end (depending on whether the CRLF comes before or after the data), and the array item count returned increases each time.

#include <File.au3>
#include <Array.au3>

$file = @TempDir& "\test.txt"
if FileExists($file) then FileDelete($file)

Dim $a[4] = [3, 1, 2, 3]

For $i = 1 To 10
	_FileWriteFromArray($file, $a, 1)
	_FileReadToArray($file, $a)
Next
_ArrayDisplay($a)

It's possible that the real issue is in _FileReadToArray, which should discard the very last CRLF it finds.

comment:3 by Gary, 18 years ago

This should fix that problem also.

	$aArray = StringSplit(StringStripCR(StringStripWS(FileRead($hFile, FileGetSize($sFilePath)),2)), @LF)

comment:4 by Gary, 18 years ago

Resolution: fixed
Status: reopenedclosed

(In [2821]) Fixed #42: _FileReadToArray leaving last line feed in the array (mlowery)

Modify Ticket

Action
as closed The owner will remain Gary.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.