Page 1 of 1 1
Topic Options
#195247 - 2009-08-04 10:11 PM Endless Loop
ElGuapo Offline
Starting to like KiXtart

Registered: 2002-06-19
Posts: 100
I am looking at the open and readline functions on p. 53-54 of the KiXtart 2010 manual, and I don't understand why the code below is producing an endless loop. Mylist.txt contains only 1 line.
 Code:
 
$datafile="c:\mylist.txt"

If Open (1,"$datafile") = 0 
   $line = ReadLine(1)  
   While @error = 0
	$line = ReadLine(1)
	myfunction()
   Loop
EndIf
Exit

Function myfunction()
 ? "My Function"
EndFunction


Edited by ElGuapo (2009-08-04 10:13 PM)

Top
#195250 - 2009-08-04 10:20 PM Re: Endless Loop [Re: ElGuapo]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
You are calling MyFunction, this function will set @error to 0 if it succeeds in displaying the text so While @error = 0 will always be true. Some other action that returns something else then 0 will end the loop.

You might want to swap the Readline and MyFunction() that way you can also do stuff when it has read the first line.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#195252 - 2009-08-04 10:29 PM Re: Endless Loop [Re: Mart]
ElGuapo Offline
Starting to like KiXtart

Registered: 2002-06-19
Posts: 100
Thank you. That helps.
Top
#195253 - 2009-08-04 10:39 PM Re: Endless Loop [Re: ElGuapo]
ElGuapo Offline
Starting to like KiXtart

Registered: 2002-06-19
Posts: 100
Putting END at the end of my file is what I came up with, but I'm wondering if there is a more elegant way to do this?

 Code:
$datafile="c:\mylist.txt"

If Open (1,"$datafile") = 0 
   $line = ReadLine(1)  
   While $line <> "END"
	  	myfunction()
		$line = ReadLine(1)
   Loop
EndIf
Exit

Function myfunction()
 ? "ResetVariables"
EndFunction

Top
#195254 - 2009-08-04 10:58 PM Re: Endless Loop [Re: ElGuapo]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
A small example.

 Code:
$datafile = "c:\mylist.txt"

If Open(1, $datafile, 2) = 0 
	$line = ReadLine(1)  
	While Not @ERROR
		? "Reset variables"
		Sleep 2
		$line = ReadLine(1)
	Loop
EndIf


Mylist.txt:
 Quote:

Line1
Line2
Line3
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#195283 - 2009-08-05 10:25 PM Re: Endless Loop [Re: ElGuapo]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
To elaborate just a bit on Mart's suggestion, when you create a loop that's controlled by @ERROR being false, you MUST place the command or function that will set the error you are interested in LAST in the loop code. For example:
 Code:
$Line = ReadLine(2)
While Not @ERROR ; do this stuff until an error occurs
  ; stuff goes here to process $Line
  $Line = ReadLine(2) ; will set @ERROR when no more data is available (EOF Error)
Loop
Since you want to read the data until there's no more data to read, the ReadLine will return 0 when it has read data, and an error when it has no more data due to encountering the End Of File. Note the use of negative logic in "While NOT @ERROR" - loop as long as no error exists.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#195290 - 2009-08-05 11:34 PM Re: Endless Loop [Re: Glenn Barnas]
ElGuapo Offline
Starting to like KiXtart

Registered: 2002-06-19
Posts: 100
Thank you again.
Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 557 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.06 seconds in which 0.025 seconds were spent on a total of 14 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org