DODODO

Why is EOF never reached?

7 posts in this topic

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)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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.

Share this post


Link to post
Share on other sites

yes, it lists out a line from the file so defo can find/open it

Share this post


Link to post
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.

Share this post


Link to post
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 :)


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

OK, i copy and pasted your example and it works.  Which is odd as mine didn't, so am wondering if it is a simple copy/paste error.

 

Thanks for the assist!

Share this post


Link to post
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.

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