Modify

Opened 12 years ago

Closed 12 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 Changed 12 years ago by Gary

  • Milestone set to 3.2.11.0
  • Resolution set to fixed
  • Status changed from new to closed

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 Changed 12 years ago by mlowery

  • Resolution fixed deleted
  • Status changed from closed to reopened

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 Changed 12 years ago by Gary

This should fix that problem also.

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

comment:4 Changed 12 years ago by Gary

  • Resolution set to fixed
  • Status changed from reopened to closed

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

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.