[RESOLVED] Flush data in output file
I have to write logging information into a file:
VB Code:
Open "C:\pgm.log" For Output As #1
...
print#1, "debug data"
...
close #1
Is there a way to force VB to flush the buffer to the disk once in a while? I could not find any flush function for files. Right now I have to close and re-open the file with append after every write, which is much too slow.
Re: Flush data in output file
If you want your program to up date automatically then you can stick your print code in a timer.
VB Code:
Private Sub Timer1_Timer()
Dim handle As Integer
handle = FreeFile
Open "C:\Documents and Settings\Carl\My Documents\Programming\List1.txt" For Append As handle
'//.....
Print #handle, "Data Log 1"
'//.....
Close #handle
End Sub
My Timers interval is set to 1000 (1 second) so my file will update every second. I hope this helps you.
Jenova
Re: Flush data in output file
The problem is that I could attempt to log something in the file while the timer closed the file to re-open it with append.
Still I think closing/appending to a file is a crapy way of forcing the buffers to flush to the disk. I wish that was a better way.
Re: Flush data in output file
Quote:
Originally Posted by JacquesLebrun
The problem is that I could attempt to log something in the file while the timer closed the file to re-open it with append.
Still I think closing/appending to a file is a crapy way of forcing the buffers to flush to the disk. I wish that was a better way.
You don't have to open and close it for every write.
Open it in your Form Load event.
Put the code to write to it in the timer event.
Close the file in your Form Unload event.
Re: Flush data in output file
Hi,
I'm looking for the same thing. :)
I know if can be done with API calls, but then you need to write to the file via api calls too :/
If I find it I'll post up here.
Re: Flush data in output file
Quote:
Originally Posted by JacquesLebrun
Still I think closing/appending to a file is a crapy way of forcing the buffers to flush to the disk. I wish that was a better way.
Very true :( But I think that's the only way to do it. What I do is set up a counter variable and after every x number of writes I close & re-open the file. Cumbersome & ugly, but it works.