Jump to content

Help with FileReadLine()


Recommended Posts

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 1

Line 2

Line 3

10

Thanks for your help,

Ian

Edit: Here's a link to the problem I was working on : http://www.autoitscript.com/forum/index.php?showtopic=7371

Edited by ioliver

"Blessed be the name of the Lord" - Job 1:21Check out Search IMF

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

$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)

Link to comment
Share on other sites

  • Developers

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.
  :)

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...