Solved

Read from text file that is being changed

Posted on 2004-09-22
14
162 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.net 2008 2 59
VBA open file from excel cell 4 42
String manipulation in Visual Basic 7 62
Determine Range to Select 5 48
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

777 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