bills Posted June 28, 2007 Share Posted June 28, 2007 (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 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 June 28, 2007 by bills Link to comment Share on other sites More sharing options...
herewasplato Posted June 28, 2007 Share Posted June 28, 2007 (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 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? //BillFileOpen 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 June 28, 2007 by herewasplato [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
bills Posted June 28, 2007 Author Share Posted June 28, 2007 I did this : Func Button() FileOpen($log, 2) FileWrite($log, $writenow + 1) FileClose($log) EndFunc And suddenly it works Thanks guys ! Link to comment Share on other sites More sharing options...
MHz Posted June 28, 2007 Share Posted June 28, 2007 (edited) I did this : Func Button() FileOpen($log, 2) FileWrite($log, $writenow + 1) FileClose($log) EndFunc And suddenly it works 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. Edited June 28, 2007 by MHz Link to comment Share on other sites More sharing options...
MHz Posted June 28, 2007 Share Posted June 28, 2007 Note: Correct previous reply. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now