Link to home
Start Free TrialLog in
Avatar of n96radsb
n96radsb

asked on

Read from text file that is being changed

I want to read from a text file that is being changed.

There is a program that writes to a text file. The text file is being updated almost every other second.

I want in my program to read from the text-file and place every line in an array, this has to be done pretty quick.

I was thinking to have a loop ,so every second, there is a command that reads from the text file. Preferably to read the last 10 lines from the text-file. Compare those lines to the the lines from the previous "read" and then write the lines that are new to an array.

Then display the last line in a text-field, that is being every other second.

Is this a good way?

Im having problems with:

 reading from just the last 10 lines in the file

To run the loop just once every second. (the form never shows up when I try to run my program, since it seems busy with the loop...)

thanks in advance!

//Björn
Avatar of aelatik
aelatik
Flag of Netherlands image

Post you code, we can optimize it for you...
ASKER CERTIFIED SOLUTION
Avatar of aelatik
aelatik
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Initially, read all the lines into your array.  Then, store the number of lines you read in a variable.  (You can use UBound to figure this out or count the lines as you go.)

Now on each subsequent read, you can use that value to skip the number the lines you already read.  Everything beyond that can be retrieved using "Input line #X...".

Don't forget to update your last line number read variable.

Regards,

Idle_Mind
Avatar of n96radsb
n96radsb

ASKER

Okay this is what I written so far, but the form never shows up on the screen



Private Sub Form_Load()
Dim x As Integer
Dim stored(10000) As String
Dim s As Double


 Do
        s = Timer + 0.1
        While s > Timer
            DoEvents
        Wend

'''' reads from the file and puts it in stored-array
Open "C:\text.txt" For Input As #1
x = 0
    Do While Not EOF(1)
        Input #1, stored(x)
        x = x + 1
    Loop


Close #1

'''''''''''''''''''''''''''''''''''''''''''''''''''''
Text1.Text = stored(x - 1)

Loop


End Sub
Try this :

Private Sub Form_Load()
    Me.Show
    DoEvents
    Dim x As Integer
    Dim stored(10000) As String
    Dim s As Double
    Do
        s = Timer + 0.1
        While s > Timer
            DoEvents
        Wend

    Open "C:\text.txt" For Input As #1
    x = 0
    Do While Not EOF(1)
        Input #1, stored(x)
        x = x + 1
    Loop
    Close #1
    Text1.Text = stored(x - 1)
Loop
End Sub
thanks aelatik
Now it worked, was it just Me.show that was added?

The function GetLastLines also worked. at first. Well it worked while the other program wasnt writing to the file. When I started that program I get an error message from my program

run time error 70
permission denied

The error message is cause because of this line :

Open Filename For Binary Access Read Write As #FNUM

change that to :

Open Filename For Binary Access Read As #FNUM

' The file is locked so you can only open it in READ mode
As a side note, your application will use less CPU resources if you use a Timer control instead of a polling loop.

Your delaying code:
        s = Timer + 0.1
        While s > Timer
            DoEvents
        Wend

will cause the CPU usage to ramp to 100%.  Your app will still respond because of the DoEvents but you can ease the load a little by using the Timer control instead.

Idle_Mind
Put a timer control on you form and add this ( its better than a DO loop ) :

Dim LastLine As String

Private Sub Form_Load()
    LastPos = 1
    Timer1.Interval = 1000 ' 1 second
End Sub

Private Sub Timer1_Timer()
    Dim BUFFER As String
        BUFFER = Space(FileLen("C:\text.txt"))
    Dim LINE_ARRAY() As String
    Open "C:\text.txt" For Binary Access Read Write As #1
        Get #1, 1, BUFFER
    Close #1
        LINE_ARRAY = Split(BUFFER, vbCrLf)
        Debug.Print LINE_ARRAY(UBound(LINE_ARRAY))
End Sub
Sorry Idle_Mind, I should refreshed the Q have first :-) ...
thanks guys
aelatik and idle mind.  I'll try with a timer. and get back if I have any more problems, I really appreciate your help.

/B
Didnt really understand how to implement the timer instead of the delay loop...


Private Sub Form_Load()


    Me.Show
    DoEvents
    Dim x As Integer
    Dim stored(10000) As String
    Dim s As Double
   
    Do
        s = Timer + 0.1
        While s > Timer
            DoEvents
        Wend

 
    Text1.Text = GetLastLines(1, "C:\test.txt")
 
    Loop


End Sub



Function GetLastLines(ByVal LineCount As Long, ByVal Filename As String) As String
    Dim MyFile As String: MyFile = Space(FileLen(Filename)): Dim BUFFER As String
    Dim MyArray() As String: Dim FNUM As Integer: FNUM = FreeFile: Dim i As Integer
    Open Filename For Binary Access Read As #FNUM
        Get #FNUM, 1, MyFile
    Close #FNUM
    MyArray = Split(MyFile, vbCrLf)
    If UBound(MyArray) > LineCount Then
        For i = UBound(MyArray) - LineCount To UBound(MyArray)
            If i <> UBound(MyArray) Then BUFFER = BUFFER & MyArray(i) & vbCrLf Else BUFFER = BUFFER & MyArray(i)
        Next
    Else
        For i = LBound(MyArray) To UBound(MyArray)
            If i <> UBound(MyArray) Then BUFFER = BUFFER & MyArray(i) & vbCrLf Else BUFFER = BUFFER & MyArray(i)
        Next
    End If
    GetLastLines = BUFFER
End Function
Remove all code > Add a timer and a text control and paste the following :

Function GetLastLines(ByVal LineCount As Long, ByVal Filename As String) As String
    Dim MyFile As String: MyFile = Space(FileLen(Filename)): Dim BUFFER As String
    Dim MyArray() As String: Dim FNUM As Integer: FNUM = FreeFile: Dim i As Integer
    Open Filename For Binary Access Read As #FNUM
        Get #FNUM, 1, MyFile
    Close #FNUM
    MyArray = Split(MyFile, vbCrLf)
    If UBound(MyArray) > LineCount Then
        For i = UBound(MyArray) - LineCount To UBound(MyArray)
            If i <> UBound(MyArray) Then BUFFER = BUFFER & MyArray(i) & vbCrLf Else BUFFER = BUFFER & MyArray(i)
        Next
    Else
        For i = LBound(MyArray) To UBound(MyArray)
            If i <> UBound(MyArray) Then BUFFER = BUFFER & MyArray(i) & vbCrLf Else BUFFER = BUFFER & MyArray(i)
        Next
    End If
    GetLastLines = BUFFER
End Function

Private Sub Form_Load()
    Timer1.Interval = 1000
End Sub

Private Sub Timer1_Timer()
    Text1.Text = GetLastLines(0, "C:\Program Files\EmpirePoker\HandHistory\bjora999\20040922\Table  12421.txt")
End Sub
I would change your timer sub to the below just in case it takes longer than one second to read the file:

Private Sub Timer1_Timer()
    Timer1.Enabled = False
    Text1.Text = GetLastLines(0, "C:\text.txt")
    Timer1.Enabled = True
End Sub