?
Solved

Read from text file that is being changed

Posted on 2004-09-22
14
Medium Priority
?
167 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
14 Comments
 
LVL 14

Expert Comment

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

Accepted Solution

by:
aelatik earned 560 total points
ID: 12123543
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 86

Expert Comment

by:Mike Tomlinson
ID: 12123544
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:n96radsb
ID: 12123579
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
ID: 12123600
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
ID: 12123753
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
ID: 12123766
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12123793
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
ID: 12123820
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
ID: 12123835
Sorry Idle_Mind, I should refreshed the Q have first :-) ...
0
 

Author Comment

by:n96radsb
ID: 12124031
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
ID: 12124282
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
ID: 12124573
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 86

Expert Comment

by:Mike Tomlinson
ID: 12125986
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month10 days, left to enroll

762 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