Results 1 to 6 of 6

Thread: [RESOLVED] Flush data in output file

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    263

    Resolved [RESOLVED] Flush data in output file

    I have to write logging information into a file:

    VB Code:
    1. Open "C:\pgm.log" For Output As #1
    2. ...
    3. print#1, "debug data"
    4. ...
    5. 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.

  2. #2
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    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:
    1. Private Sub Timer1_Timer()
    2.     Dim handle As Integer
    3.    
    4.     handle = FreeFile
    5.    
    6.     Open "C:\Documents and Settings\Carl\My Documents\Programming\List1.txt" For Append As handle
    7.         '//.....
    8.         Print #handle, "Data Log 1"
    9.         '//.....
    10.     Close #handle
    11. End Sub

    My Timers interval is set to 1000 (1 second) so my file will update every second. I hope this helps you.

    Jenova

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    263

    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.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  5. #5
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    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.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  6. #6
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    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.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width