Jump to content
Sign in to follow this  
ioliver

Help with FileReadLine()

Recommended Posts

ioliver

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

Share this post


Link to post
Share on other sites
HighGuy

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?

Share this post


Link to post
Share on other sites
SlimShady

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

Share this post


Link to post
Share on other sites
Jos

When the last line is read the loop continues

Then it hits EOF and FileReadLine returns "" and @error is set to -1


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
ioliver

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

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.