text: Opened 11 years ago
  Closed 11 years ago
  #42 closed Bug (Fixed)
  _FileWriteFromArray outputs too many CRLF (File.au3)
  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 11 years ago by Gary
  - Milestone set to 3.2.11.0
  - Resolution set to fixed
  - Status changed from new to closed
  comment:2 Changed 11 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 11 years ago by Gary
  This should fix that problem also.
  $aArray = StringSplit(StringStripCR(StringStripWS(FileRead($hFile, FileGetSize($sFilePath)),2)), @LF)
  comment:4 Changed 11 years ago by G.
  Already Fixed in 3.2.11.0