Sign in to follow this  
Followers 0
Alandor

FileClose or FileWriteLine problem

5 posts in this topic

#1 ·  Posted (edited)

I would like to know if this is normal, becausse I can't understand why it gets me some error.

The error consist in the writed file not being closed. And not only that, the file is increasing its size at a high speed with "blank" lines. I think it enter in a endless loop.

I tried with While 1 (like the manual example) and with all I know it could be, but the error still there. Could this be a bug ??

The part of code that generate the error is this:

If FileExists ($samurizeConfigFile) Then 

    $ficheroLectura = FileOpen($samurizeConfigFile, 0)
    $ficheroEscritura = FileOpen($samurizeConfigFile & ".bak.ini", 2)
    
    Do
    
        $linea = FileReadLine($ficheroLectura)
        If $linea = "AlertValue=00:00:00" Then $linea = "AlertValue=" & $horas & ":" & $minutos & ":" & $segundos
        FileWriteLine($ficheroEscritura, $linea & @CRLF)
    
    Until @error = -1
    
    FileClose($ficheroLectura)
    FileClose($ficheroEscritura)
    
EndIf

Thanx,

- Alandor -

Edited by Alandor

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

This is strange, with the while:

While 1
    
        $linea = FileReadLine($ficheroLectura)
        If $linea = "AlertValue=00:00:00" Then $linea = "AlertValue=" & $horas & ":" & $minutos & ":" & $segundos
        FileWriteLine($ficheroEscritura, $linea)
    
WEnd

I got the same error, but I tried a Do Until 1 instead of the other both and It works, what causes the error ??

Do
    
        $linea = FileReadLine($ficheroLectura)
        If $linea = "AlertValue=00:00:00" Then $linea = "AlertValue=" & $horas & ":" & $minutos & ":" & $segundos
        FileWriteLine($ficheroEscritura, $linea)
    
    Until 1

Sorry, I talked before contrast it. With this last it only repeat the secuence of commands once. :)

This is going to get me crazy......... What can I do ??

Edited by Alandor

Share this post


Link to post
Share on other sites

You never actually check to see if you are reaching EOF. Thus, you are writing a blank string, which combined with the automatically appended CRLF of FileWriteLine(), you are in an infinite loop of writing blank lines. You have to check @error immediately after calling FileReadLine() and if it is set, then exit from the loop.

Share this post


Link to post
Share on other sites

Thanx for your help. Mmmmm, then the @error macro has to be checked inmediately after the function that change its value is called. I didn't knew that. :) Thanx. However, Is it possible to check the EOF from another way ?? I don't like too much the use of a exit Loop call, It seems to me very unclean in the main code (just like a basic goto call). Is there another way more cleaner ??

Thanx again for your help :)

- Alandor -

Share this post


Link to post
Share on other sites

ExitLoop isn't even on the same planet as goto unless you're using it to jump out of multiple nested loops, in which case, it gets to be hard to follow (But still, nowhere near goto). With a call to ExitLoop, you always know that code execution will resume at the end of the loop its breaking out of, not in some arbitrary place 300 lines later, if even in the same file.

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  
Followers 0