ioliver Posted January 7, 2005 Posted January 7, 2005 (edited) I'm trying to help someone esle out with a problem, and now I'm stuck...$file = FileOpen("c:\counter.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) If @error = -1 Then ExitLoop Wend FileClose($file) MsgBox(0, "Line:", $line) $counter = $line + 1 MsgBox(0,"Counter:", $counter)In the above code, shouldn't $line = the Last Line in the file "c:\counter.txt"? Counter.txt is just a txt file that reads:Line 1Line 2Line 310Thanks for your help,IanEdit: Here's a link to the problem I was working on : http://www.autoitscript.com/forum/index.php?showtopic=7371 Edited January 7, 2005 by ioliver "Blessed be the name of the Lord" - Job 1:21Check out Search IMF
HighGuy Posted January 7, 2005 Posted January 7, 2005 Change your code to something like (not tested): $file = FileOpen("c:\counter.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) If @error = -1 Then ExitLoop Else $i = $line EndIf Wend FileClose($file) $counter = $i + 1 MsgBox(0,"Counter:", $counter) When @error occures $line is not defined. You have to save the last line in a seperate variable. Does it work now?
SlimShady Posted January 7, 2005 Posted January 7, 2005 $file = FileOpen("c:\counter.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 $curline = FileReadLine($file) If @error = -1 Then ExitLoop $line = $curline Wend FileClose($file) MsgBox(0, "Line:", $line) $counter = $line + 1 MsgBox(0,"Counter:", $counter)
Developers Jos Posted January 7, 2005 Developers Posted January 7, 2005 When the last line is read the loop continues Then it hits EOF and FileReadLine returns "" and @error is set to -1 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
ioliver Posted January 7, 2005 Author Posted January 7, 2005 Thanks to everyone for your replies... You can also use _FileLineCount(), and avoid the While ... Wend loop. Thanks again, Ian "Blessed be the name of the Lord" - Job 1:21Check out Search IMF
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now