Sign in to follow this  
Followers 0
NSpeare

Problem with FileOpen and FileReadLine (should be a SIMPLE fix)

2 posts in this topic

Hey everybody, trying to put together my first AutoIt script today. I was really excited to find AutoIt because it seems to have exactly the features I was looking for, so I really hope we can get this figured out.

Anyway, my basic problem is this: I am trying to read lines from a plain text file, and AutoIt seems to think that there are a bunch of extra blank lines at the end.

Here is the script I am using:

$file=FileOpen("C:\Users\Nick\Desktop\AutoIt\checklist.txt",0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $line = StringStripWS(FileReadLine($file),3)
    If @error = -1 Then ExitLoop
    MsgBox(0, "Line read:", $line)
Wend

FileClose($file)

Exit

The checklist.txt file that is being read is just a simple text file that says:

ONE
TWO
THREE

So when I execute the script it brings up the three msgboxes as expected, saying ONE, TWO, and THREE, but then it keeps bringing up additional blank msgboxes ad infinitum until I have to cancel the execution.

This is driving me crazy! Help ??

Thanks,

Nick

Share this post


Link to post
Share on other sites



You're right. That fixed it. Thanks for your help!

The problem is that the call to StringStripWS is overwriting any error from FileReadLine so the Exitloop will never be triggered. You just need to make some minor changes to the order of the commands.

$file = FileOpen("checklist.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then Exit MsgBox(0, "Error", "Unable to open file.")

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $line = StringStripWS($line, 3)
    MsgBox(0, "Line read:", $line)
WEnd

FileClose($file)

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