Opened 18 years ago
Closed 18 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)
follow-up: 2 comment:1 by , 18 years ago
| Severity: | → None |
|---|
comment:2 by , 18 years ago
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 by , 18 years ago
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.
follow-up: 5 comment:4 by , 18 years ago
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 by , 18 years ago
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 by , 18 years ago
| Milestone: | → 3.2.13.0 |
|---|---|
| Owner: | set to |
| Resolution: | → Fixed |
| Status: | new → closed |
Fixed in version: 3.2.13.0

Fails on files that contain CRLF.
For example use @ScriptFullPath as the file to parse.