Re: Open append text files for update simultaneously
- From: "Someone" <nobody@xxxxxxx>
- Date: Tue, 11 Oct 2005 16:23:03 -0400
I forgot a line that I would have added before this line:
Print #f1, LineToAdd ' Use "Print #f1, LineToAdd;" if you don't want
The line to add before the line above is:
Seek #f1, LOF(f1) + 1
This moves the file pointer after the last character in the file. This is
because another app could have added extra characters since the last time
you use Print #f1. Your app might still think that it's at the end of the
file when it's not, resulting in overwriting part of what the other app has
added. VB6 doesn't move the file pointer when other apps append to the file.
The line above ensures that the text is added at the end of the file, and
the Lock statement ensures that no other app or function can write to the
file, even if Windows switched to that app or another app tried to access
that file from the network.
"Someone" <nobody@xxxxxxx> wrote in message
news:hCU2f.5186$MN6.3782@xxxxxxxxxxxxx
> Try the following sample, adjust as needed. Mainly, it's best to keep the
> file open rather than opening and closing it for every line. Please don't
> use Notepad as a testing tool to "lock" the file. Notepad reads the file
> into memory and close it in a split second. Use MS Word instead or similar
> software, which keeps the file open all the time while you are editing it.
>
> Option Explicit
>
> Private Sub Form_Load()
> ' How to use
> AppendLine "C:\Hello.txt", Format(Now, "mm/dd/yyyy Hh:Nn:Ss AM/PM") &
> ": Test1"
> AppendLine "C:\Hello.txt", Format(Now, "mm/dd/yyyy Hh:Nn:Ss AM/PM") &
> ": Test2"
> End Sub
>
> Private Sub AppendLine(ByRef FileName As String, ByRef LineToAdd As
> String)
> On Error Resume Next
> Dim f1 As Integer
> Dim i As Long
> Dim Locked As Boolean
>
> f1 = FreeFile
> Open FileName For Append Access Read Write Shared As #f1
> If Err.Number <> 0 Then
> MsgBox "AppendLine: Cannot Open/Create " & FileName & vbCrLf &
> "Error " & Err.Number & ": " & Err.Description
> Err.Clear
> Exit Sub
> End If
> ' Retry locking up to 100 times
> Locked = False
> For i = 1 To 100
> Lock #f1 ' Lock the entire file
> If Err.Number <> 0 Then
> ' Lock failed
> Debug.Print "AppendLine: Lock retry "; i; " failed."
> Err.Clear
> ' Wait 0.05 second before trying again
> Delay 0.05
> Else
> ' Successfully locked
> Locked = True
> Exit For
> End If
> Next
>
> If Locked Then
> ' Write the line
> Print #f1, LineToAdd ' Use "Print #f1, LineToAdd;" if you don't
> want a new line at the end
> Unlock #f1
> Else
> ' All retries failed to lock the file
> MsgBox "AppendLine: Cannot lock file."
> End If
>
> Close f1
>
> End Sub
>
> Private Sub Delay(ByVal Seconds As Single)
> Dim t As Single
>
> t = Timer
> Do While Timer - t < Seconds
> If t > Timer Then ' Adjust for midnight rollover to 0
> t = t - 86400
> End If
> Loop
>
> End Sub
>
>
>
>
> <chook.harel@xxxxxxxxx> wrote in message
> news:1129056121.497759.176130@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>>I read a msg after trying to find the reason for append shared and how
>> it really works.
>>
>> If you can help me,
>> I need to build a program that will fill a text file, which means,
>> Append certain text to it...
>> Now this program should run on several computers and each one of them
>> should append to the same file
>> Can visual basic handle it? Or it will give me errors when two tries to
>> write at the same time.
>>
>> Now moreover, a second process should simultaneously read from the file
>> (needs to read the first line like in a FIFO stack)
>>
>> Thanks in advance,
>> Chen.
>>
>
>
.
- Follow-Ups:
- Re: Open append text files for update simultaneously
- From: chook . harel
- Re: Open append text files for update simultaneously
- References:
- Open append text files for update simultaneously
- From: chook . harel
- Re: Open append text files for update simultaneously
- From: Someone
- Open append text files for update simultaneously
- Prev by Date: Re: Open append text files for update simultaneously
- Next by Date: Re: Open append text files for update simultaneously
- Previous by thread: Re: Open append text files for update simultaneously
- Next by thread: Re: Open append text files for update simultaneously
- Index(es):
Relevant Pages
|