Home All Groups Group Topic Archive Search About

VB6 writing to text files requires Close to commit

Author
9 Mar 2006 5:41 PM
Bill Baker
I am using the Open method for logging in my VB6 app. This works fine,
however the Print results are not written to the text file until the Close
method is used. For example:

Open {file} for Append As #1
Print #1, "This is a test"
' Nothing has been written to the {file} at this point
Close 1
' Now the {file} reads: This is a test

Is there a better method that I can use, or is there a way to purge the
"Print" buffer without constantly opening and closing the file?

Thanks,
Bill

Author
9 Mar 2006 6:45 PM
Robert Ellis
Bill,

    Why is this limitation proving a problem?

    If you find yourself repeating code, then that code needs to be isolated
and placed into a single function or subroutine. So, you write one function
that does the OPEN, the PRINT, and the CLOSE, and then you call that
function whenever you need it.

    It is difficult to avoid the overhead of repeated OPEN/CLOSE on a file
when you are logging. Because if you don't CLOSE immediately after writing
to your log file, then in the event that your application crashes, you may
not end up with a log-file at all. That's why most real-world applications
have a switch or option that allows you to turn logging on or off - it hurts
performance.

    I have been flamed before for suggesting this, but you may wish to take
a look at the Microsoft Scripting Runtime library (scrrun.dll) and see if
that works differently or helps you. Some folks feel that referencing this
library from a VB6 application is not a good idea, but personally, I just
run with whatever works effectively and allows for quick code development,
and I've made good use of it in real world applications with no reliability
problems whatsoever.

    Or you can dig into the Windows APIs...

    Anyway... I'm rambling...

    HTH,,  Robert


Show quoteHide quote
"Bill Baker" <W*@discussions.microsoft.com> wrote in message
news:131EB932-6D28-4E79-8437-69AB642066B9@microsoft.com...
>I am using the Open method for logging in my VB6 app. This works fine,
> however the Print results are not written to the text file until the Close
> method is used. For example:
>
> Open {file} for Append As #1
> Print #1, "This is a test"
> ' Nothing has been written to the {file} at this point
> Close 1
> ' Now the {file} reads: This is a test
>
> Is there a better method that I can use, or is there a way to purge the
> "Print" buffer without constantly opening and closing the file?
>
> Thanks,
> Bill
Author
9 Mar 2006 6:54 PM
Tom Esh
On Thu, 9 Mar 2006 09:41:32 -0800, Bill Baker
<W*@discussions.microsoft.com> wrote:

>...
>Is there a better method that I can use, or is there a way to purge the
>"Print" buffer without constantly opening and closing the file?

Nope. You would need to use API instead of VB file methods. You could
also consider a comprimise - Close / re-open every n writes rather
than after each one.


-Tom
MVP - Visual Basic
(please post replies to the newsgroup)
Author
10 Mar 2006 11:44 AM
J French
On Thu, 09 Mar 2006 13:54:12 -0500, Tom Esh
<tjeshGibber***@suscom.net> wrote:

>On Thu, 9 Mar 2006 09:41:32 -0800, Bill Baker
><W*@discussions.microsoft.com> wrote:
>
>>...
>>Is there a better method that I can use, or is there a way to purge the
>>"Print" buffer without constantly opening and closing the file?

>Nope. You would need to use API instead of VB file methods. You could
>also consider a comprimise - Close / re-open every n writes rather
>than after each one.

There is another method that tends to work, if one is writing into the
middle of a pre-extended file then the directory entry size does not
need updating
- it is not ideal for log files
- but it is a good idea when updating valuable data files

If logging for errors I would certainly consider pre-extending the
file.