Solved

Read from text file that is being changed

Posted on 2004-09-22
14
164 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
ID: 12123287
Post you code, we can optimize it for you...
0
 
LVL 14

Accepted Solution

by:
aelatik earned 140 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 85

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 85

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 85

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to debug this code 7 65
Modifying Conditional Format from VBA code 3 69
Problem to With line 4 73
pop out of webbrowser1 control vba6 5 41
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

685 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