Solved

Read from text file that is being changed

Posted on 2004-09-22
14
159 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:n96radsb
  • 7
  • 4
  • 3
14 Comments
 
LVL 14

Expert Comment

by:aelatik
Comment Utility
Post you code, we can optimize it for you...
0
 
LVL 14

Accepted Solution

by:
aelatik earned 140 total points
Comment Utility
You are probable opening the file with normal file INPUT mode. If you open the file for binary access it will load much faster.

I made a function that retrieves the last lines as specified. Just try it...



Private Sub Command1_Click()
    MsgBox GetLastLines(12, "c:\windows\iis6.log") 'Gets the last 12 lines of the specied file and passes it back to the function...
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 Write 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


0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
0
 

Author Comment

by:n96radsb
Comment Utility
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
0
 
LVL 14

Expert Comment

by:aelatik
Comment Utility
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
0
 

Author Comment

by:n96radsb
Comment Utility
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

0
 
LVL 14

Expert Comment

by:aelatik
Comment Utility
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
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
0
 
LVL 14

Expert Comment

by:aelatik
Comment Utility
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
0
 
LVL 14

Expert Comment

by:aelatik
Comment Utility
Sorry Idle_Mind, I should refreshed the Q have first :-) ...
0
 

Author Comment

by:n96radsb
Comment Utility
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
0
 

Author Comment

by:n96radsb
Comment Utility
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
0
 
LVL 14

Expert Comment

by:aelatik
Comment Utility
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
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now