Jump to content

Why is EOF never reached?


DODODO
 Share

Recommended Posts

What am I not understanding?  When I run the sample code from the help file to read a text file it never seems to reach the end of the file.  I'm using the below code and the only thing it writes to the console is 1 line of the log file and I never see EOF written to the console.  I know it's me but I can't suss it out :-(

 

$file = FileOpen("C:\temp\log.log", 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
    ConsoleWrite("Line read:" & $line)
Wend
ConsoleWrite("EOF")
FileClose($file)

Link to comment
Share on other sites

I see from the help file: 

@error: 1 = if file not opened in read mode or other error

So do you have the correct path and privs for the file to open?

Edit: Add a trap for @error = 1 to see if that is the problem

Edited by SlackerAl

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Link to comment
Share on other sites

I changed my code to this (I do not have console access) :

$file = FileOpen("C:\temp\log.log", 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
    MsgBox(4096, "read", $line, 10)

Wend
MsgBox(4096, "finished", @error, 10)
FileClose($file)

I made C:\temp\log.log containing two lines:

test 1
test 2

This ran exactly as I would have expected. Two lines then error -1. What does it do for you?

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Link to comment
Share on other sites

$file = FileOpen("C:\temp\log.log", 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
    If @error Then MsgBox(0, "Error", "FileReadLine has set @error to: " & @error)
    ConsoleWrite("Line read:" & $line)
Wend
ConsoleWrite("EOF")
FileClose($file)

Try this code and see if you catch an error message :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

EOF is reached in the code in first post.

Simply add " & @CRLF " (without quotation marks) immediately before the end parenthesis in the ConsoleWrite statements.

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