Sign in to follow this  
Followers 0
Colin

_FileReadToArray adding a blank element at index [1]

6 posts in this topic

#1 ·  Posted (edited)

I noticed this when I was using _FileWriteFromArray and the file would have an extra blank line at the top. I used both a MsgBox and _ArrayDisplay just after the _FileReadToArray line just to make sure it was the Read and not the Write. Indeed, the element with index [1] in the array is blank. I don't know if this is a common issue or if it's just me.

Unfortunately I can't send the .txt file I am working on as it contains actual client data along with passwords. Obviously, there are no noticable characters at the front of the first line. The only thing that might be of note would be that I am using pipes, |, to separate data inside the text file, but I'm not splitting the string in this portion of the code, and I don't see how that could actually affect anything.

Here's the relevant code:

(Of course with the code in there for testing it doesn't quite work how it would normally. Commenting or removing the lines which are labeled with ";Test Array" will give you the normal code.)

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

;Testing
$username = "test2"
$email = "email2"
$firstname = "fname2"
$lastname = "lname2"
$company = "comp2"
$city = "city2"
$state = "state2"
$type = "type2"

;Path and name of the file with data and passwords
$PasswordFile = "pwd.txt"

;Store the contents of the file pwd.txt into an array
Dim $PasswordArray
_FileReadToArray($PasswordFile, $PasswordArray)

;Test Array
_ArrayDisplay($PasswordArray, "$PasswordArray")

;Check for the first line number which contains only 8 characters. 
;This will contain the new password, and will become the line to write the new data on.
Dim $LineNum = 0
Do
    MsgBox(1, "", $PasswordArray[$LineNum]);Test Array
    $LineNum = $LineNum + 1
Until StringLen($PasswordArray[$LineNum]) = 8

;The new password
$Password = $PasswordArray[$LineNum]

;Store the new data onto the line in the array.
$PasswordArray[$LineNum] = $username & "|" & $Password & "|" & $email & "|" & $firstname & "|" & $lastname & "|" & $company & "|" & $city & "|" & $state & "|" & $type & "|"

;Write the array with the new data back to the file pwd.txt
_FileWriteFromArray($PasswordFile, $PasswordArray, 2)

Oh, and btw, I thought of a work around using _ArrayDelete($PasswordArray, 1) but that seems like a cop out, and I would worry that this problem might "fix itself" and that work around would create more problems.

Edit: Sorry, I should really do even more testing before I post here... Anyways, upon further testing, it seems that the problem is not with _FileReadToArray (or at least not always). Now, when I am pulling the _ArrayDisplay, there is no blank element. But, when it is written to the file, there is still a blank line at the top.

I'm look at the actual function in the include file (File.au3) and this code concerns me:

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

Wouldn't that mean it will add @CRLF in front of everything? Would this not cause my problem? How would I have that only enter @CRLF on the second iteration? I'm conerned as to why this is like this in the include file. What case would you want to write @CRLF to the front of the file? Maybe I'm missing something, but my guess is that this needs to be fixed for everyone...

Anyways, here's my proposed solution that seems to work, although it doesn't seem very elegant:

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

Sorry for the long post. Let me know if anyone has any comments or solutions. Thanks!

Edited by Colin

Share this post


Link to post
Share on other sites



You get the [1] element empty because there is an empty line on top of your text file.

_FileWriteFromArray splits the file content using @CRLF and returns the array created by this split. Empty element = empty line


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Yea, I'm pretty sure I mentioned that there was no empty line at the front of my file. Also, just a side note, if you look at the File.au3 include file at the _FileReadToArray (which is the function that does the splitting) it uses StringSplitCR() and then splits it using @LF (I'm guessing StringSplitCR() also removes the @LF character).

But anyways, I'm pretty positive the issue is with _FileWriteFromArray (the function that takes the array and writes it back to the file). In the code I posted, it was adding @CRLF to the front of the first line. It should only add that to lines 2+.

In any case, thanks for the speedy reply. In a lot of cases, that response would have helped me. I've missed small things like that so many times.

Share this post


Link to post
Share on other sites

I don't know how your file looks like but when I tested your code with a "normal" text file I haven't got any empty lines on top.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

What version of AutoIt are you using?

I believe this was fixed some time ago.


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

Yea, just upgraded to the most current beta release and it apparently works without my fix. Although, it does add a line to the end of the text file. Not a problem for me in this particular case but may be for others? Either way, while the fix implemented in the new version is less code, my fix is more accurate. Correct me if I'm wrong.

Thank you for the response!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0