Modify

Opened 17 years ago

Closed 17 years ago

#294 closed Bug (Fixed)

Fix for _FileFreadToArray()

Reported by: GEOSoft Owned by: Gary
Milestone: 3.2.13.0 Component: AutoIt
Version: 3.2.12.0 Severity: None
Keywords: File.au3 Cc:

Description

Fixes the situation where the file contains no @LF
NOTE: This also includes my version with a default value for $aArray, Reason= It can be called without pre-declaring the array. You just need $MyArray = _FileReadToArray("My\File\Path.ext") My feelings won't be hurt if that's removed.

Func _FileReadToArray($sFilePath, $aArray = "")
   Local $hFile
   $hFile = FileOpen($sFilePath, 0)
   If $hFile = -1 Then ;; unable to open the file
      SetError(1)
      Return 0
   EndIf
   $aFile = FileRead($hFile, FileGetSize($sFilePath));; Read the file and remove trailing white spaces
   $aFile = StringStripWS($aFile, 2)
   FileClose($hFile)
   If StringInStr($aFile, @LF) Then
      $aArray = StringSplit( StringStripCR($aFile), @LF)
   ElseIf StringInStr($aFile, @CR) Then ;; @LF does not exist so split on the @CR
      $aArray = StringSplit( $aFile, @CR)
   Else ;; unable to split the file
      SetError(1)
      Return 0
   EndIf
   Return $aArray
EndFunc  ;<==> _FileReadToArray()

Attachments (0)

Change History (6)

comment:1 follow-up: Changed 17 years ago by Gary

  • Severity set to None

Fails on files that contain CRLF.

For example use @ScriptFullPath as the file to parse.

comment:2 in reply to: ↑ 1 Changed 17 years ago by GEOSoft

Replying to Gary:

Fails on files that contain CRLF.

For example use @ScriptFullPath as the file to parse.

I'll test it again. It can probably be solved by changing

If StringInStr($aFile, @LF)

to

If StringInStr($aFile, @CRLF)

comment:3 Changed 17 years ago by GEOSoft

That wasn't the problem. In order to keep it backwards compatable I had to add the ByRef back in which means that the array has to be pre-declared again. Wish we could fix that issue). This works.

Func _FileReadToArray($sFilePath, ByRef $aArray)
   Local $hFile, $aFile
   $hFile = FileOpen($sFilePath, 0)
   If $hFile = -1 Then ;; unable to open the file
      SetError(1)
      Return 0
   EndIf
   ;; Read the file and remove any trailing white spaces
   $aFile = FileRead($hFile, FileGetSize($sFilePath))
   $aFile = StringStripWS($aFile, 2)
   FileClose($hFile)
   If StringInStr($aFile, @LF) Then
      $aArray = StringSplit( StringStripCR($aFile), @LF)
   ElseIf StringInStr($aFile, @CR) Then ;; @LF does not exist so split on the @CR
      $aArray = StringSplit( $aFile, @CR)
   Else ;; unable to split the file
      SetError(1)
      Return 0
   EndIf
   Return $aArray
EndFunc  ;<==> _FileReadToArray()

If I ignore backwards compatability then the origional fix is fine
Just call it using

$Test = _FileReadToArray(@ScriptFullPath)
If IsArray($Test) Then MsgBox(0, "TEST", Ubound($Test)-1)

That's the way I use my version but it's not backwards compatable for others.

comment:4 follow-up: Changed 17 years ago by Valik

George, why not just do something like this in your own files somewhere?

#include <File.au3>

Func _FileReadToArray2($sFilePath)
    Local $a
    _FileReadToArray($sFilePath, $a)
    SetError(@error, @extended)
    Return $a
EndFunc

comment:5 in reply to: ↑ 4 Changed 17 years ago by GeoSoft

Replying to Valik:

George, why not just do something like this in your own files somewhere?

#include <File.au3>

Func _FileReadToArray2($sFilePath)
    Local $a
    _FileReadToArray($sFilePath, $a)
    SetError(@error, @extended)
    Return $a
EndFunc

That would work as well. The way I have it now is I have a file named Arrayx.au3 which I #include and the function is actually FileReadToArray() with the 2 underscores so that works. I really think it would have been better if Jon had left the ByRef out of the first version but it's too late for that now I think.

comment:6 Changed 17 years ago by Gary

  • Milestone set to 3.2.13.0
  • Owner set to Gary
  • Resolution set to Fixed
  • Status changed from new to closed

Fixed in version: 3.2.13.0

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.