Sign in to follow this  
Followers 0
bills

Cant get the script to write to a textfile immediately :(

5 posts in this topic

#1 ·  Posted (edited)

I have a button which is supported to write to a file immediately so that i can use fileread soon after to read the vaues in that file but i cant get it to write until the program is closed :rolleyes:

I added an extra fileclose after the write but that doesnt help.

Func Button12()
    FileClose(FileWrite(FileOpen($log, 2),$writenow))
    FileClose($log)
EndFunc

Any ideas?

//Bill

Edited by bills

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I have a button which is supported to write to a file immediately so that i can use fileread soon after to read the vaues in that file but i cant get it to write until the program is closed :rolleyes:

I added an extra fileclose after the write but that doesnt help.

Func Button12()
    FileClose(FileWrite(FileOpen($log, 2),$writenow))
    FileClose($log)
EndFunc

Any ideas?

//Bill

FileOpen returns a file handle to FileWrite - ok

FileWrite returns a 1 or a 0 to FileClose - not ok

FileClose($log) uses a path when the file was opened via a file handle

From the help file under FileWrite:

Note: Do not mix filehandles and filenames, i.e., don't FileOpen a file and then use a filename in this function. Either use filehandles or filenames in your routines, not both.

From the help file under FileClose:

Upon termination, AutoIt automatically closes any files it opened, but calling FileClose is still a good idea.

Edit: Having said that, I have no idea why this works for me:

$log = "c:\temp\test.txt"
$writenow = "junkntksdafjkhlkjhdsf"
FileClose(FileWrite(FileOpen($log, 2), $writenow))
Sleep(99999)
Edit2: the file contains the data before the sleep completes.

Edit3: Okay, I figured it out - duh - the file contains the data which can be seen via notepad, but the file is still "locked open" by AutoIt so any "read attempts" by AutoIt to that file will fail until it is properly closed.

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

I did this :

Func Button()
    FileOpen($log, 2)
    FileWrite($log, $writenow + 1)
    FileClose($log)
EndFunc

And suddenly it works :rolleyes:

Thanks guys !

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I did this :

Func Button()
    FileOpen($log, 2)
    FileWrite($log, $writenow + 1)
    FileClose($log)
EndFunc

And suddenly it works :rolleyes:

Thanks guys !

Once you use FileOpen(), then you open a handle to that file. You NEED to store the handle in a variable so you can close the handle with FileClose() when finished. If you repeatedly call that function then you may end up with multiple handles open to the same file. Your code is leaving a handle open that you no longer can close until the script exists.

Func Button()
    Local $store_the_handle
    $store_the_handle = FileOpen($log, 2)
    FileWrite($store_the_handle, $writenow + 1)
    FileClose($store_the_handle)
EndFunc

Edit 1:

Amended some more detail.

Edit 2:

herewasplato notified me of code error with mixing filenames with handles. My focus with detail was not absolute with the original code posted and I have made adjustments. Sorry for any misleading information. Thanks.

:rambo:

Edited by MHz

Share this post


Link to post
Share on other sites

Note: Correct previous reply. :rolleyes:

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