Jump to content

Endless whie loop


 Share

Recommended Posts

Hi

I have used a code snippet from the help file and added one line (marked with -->)

and now it runs as an endless loop...

---------------------------------------------------

$file = FileOpen("Test.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 = FileReadLine($file)

--> MsgBox(0,"Value", @error)

If @error = -1 Then ExitLoop

MsgBox(0, "Line read:", $line)

Wend

FileClose($file)

---------------------------------------------------

Any ideas why this code runs an endless loop?

Link to comment
Share on other sites

As FuryCell stated.

Here's something from the Help File:

When entering a function @error is set to 0. Unless SetError() is called, then @error will remain 0 after the function has ended. This means that in order for @error to be set after a function, it must be explicitly set. This also means you may need to backup the status of @error in a variable if you are testing it in a While-WEnd loop.

Applied to your example:

  • The function FileReadLine() is entered, @error is reset to 0, then possibly set by the function
  • The function MsgBox() is entered (with the previous @error as part of the parameter), @error is reset to 0, then possibly set by the function.
  • You checked the value of @error (which was reset by MsgBox() ) in "If @error = -1 Then ExitLoop"

You could set a variable like $err equal to @error before calling MsgBox, and use $err instead of @error, as one possible solution.

$file = FileOpen("Test.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 = FileReadLine($file)
   $err=@error
   MsgBox(0,"Value", $err)
   If $err = -1 Then ExitLoop
   MsgBox(0, "Line read:", $line)
Wend

FileClose($file)
Edited by crashdemons

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Link to comment
Share on other sites

Hi

Thanks for the fast reply.

Do you mean that the first msgbox causes an error?

The pupose of that msgbox was to check if the line was at the last line in the

file but when it reaches tha last line it just continues.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...