CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jul 2008
    Posts
    45

    Reading text file, Please can anyone help

    I have a text file which contains several bit of data, one being the time(including seconds), and another bit containing a random 6 character/number string, for example:


    AAAAAB 1:1:1
    AAAAAC 1:1:1
    AAAAAA 1:1:2
    AAAAAB 1:1:2
    AAAAAA 1:1:2

    Note: other information is between these but these are the two which i intend to use.

    What i wish to do is make an exe which constantly runs which detects the newest times which have being added to the text file, on this example 1:1:2, and then follow by adding how many there are into a display box on the exe. For example in the sequence above the text box would display 3, but next is what i think will be trickiest, on line 3 and 5 the first 6 character/numbers are the same, so i only wish for the text box to increase by 1. Can the above be acheived and can someone prabs give me an example bit of coding or as much as i need.
    Thank you very much,
    any questions, as this is a mouth full, please reply.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Reading text file, Please can anyone help

    What have you tried so far?

    This should get you started. Split the file into lines, and work from there, using the same concepts.

    It first counts the lines, and then double-spaces the file and displays it in a msgbox.

    Code:
    Option Explicit
    
    Private Sub Form_Load()
      Dim x As Integer, st As String
      Dim ff As Integer
      Dim strBuff As String
      Dim str() As String
      ff = FreeFile
      Open App.Path & "\to do.txt" For Input As #ff
        strBuff = Input(LOF(ff), ff)
      Close #ff
      ' ----------------- two ways to skin a cat --------------
      MsgBox "Lines = " & Len(strBuff) - Len(Replace(strBuff, vbCrLf, "x")) + 1
      ' -------------------------------------------------------
      str() = Split(strBuff, vbCrLf)
      MsgBox "There are " & UBound(str) + 1 & " lines in the file"
      For x = 0 To UBound(str)
        st = st & str(x) & vbCrLf & vbCrLf
      Next x
      MsgBox st
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2008
    Posts
    70

    Re: Reading text file, Please can anyone help

    I think the tricky part of the OP is how do you stream a file in VB. He wants to listen for new writes to the file. Everything after that seems pretty straightforward to me, but I can't think of how to stream the file in VB without constantly opening and closing the file.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Reading text file, Please can anyone help

    It depends on where it's coming from. If it's being used, he can't read it.
    If you can read it every minute, that might be fine.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jul 2008
    Location
    Appleton, WI
    Posts
    23

    Re: Reading text file, Please can anyone help

    Exactly...he could include a timer in the app and have it periodically run some routines to see if the file has been changed...if it has, update the display. In the event of the file being locked, he could have it keep trying until the file is available to check for changes.

    Otherwise isn't there a way to get the properties of the file and check for the last modified date? Not sure if that is time stamped...but that could be a way to tell if something has been written to the file.

  6. #6
    Join Date
    Jul 2008
    Posts
    45

    Re: Reading text file, Please can anyone help

    The file is constantly updating and i need a program to tell me how many lines of input appear every second, but if the same code appears twice in one second, then the second and subsequent appearance are ignored.

  7. #7
    Join Date
    Jul 2008
    Location
    Appleton, WI
    Posts
    23

    Re: Reading text file, Please can anyone help

    Is the file constantly open?

  8. #8
    Join Date
    Jul 2008
    Posts
    45

    Re: Reading text file, Please can anyone help

    Yes i would like it to be constant.

  9. #9
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Reading text file, Please can anyone help

    read post #4 again. then answer that, also.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  10. #10
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Reading text file, Please can anyone help

    If you just want the newest lines and keep the file open then I am thinking you could open it for binary access shared and then check the file size in each cycle of the timer. If the size has increased read the new bytes by calculating the newfilesize-oldfilesize seek to the position of the oldfilesize and read new-old bytes then process accordingly.

  11. #11
    Join Date
    Jul 2008
    Location
    Appleton, WI
    Posts
    23

    Re: Reading text file, Please can anyone help

    If you are using the Scripting.FileSystemObject, you can always take a copy of the file...run your tests on the copy and then recopy, test, recopy, test ad nauseum...only prob will be some delay from the origional file writing to the testing. I have tested copying a file that is open with the file system object and it worked.

    Otherwise I don't think you can read from it...unless you can access the code that is writing the file and run the checks there (before the write)...but then, I'm guessin you wouldn't need to do this if you could just do that.

  12. #12
    Join Date
    Jul 2008
    Location
    Appleton, WI
    Posts
    23

    Re: Reading text file, Please can anyone help

    DataMiser, That is so clever. Don't know if I'd have the know how to implement that...but nicely done. Sounds like it would work. Actually after reading that again...I think I could do it.

  13. #13
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Reading text file, Please can anyone help

    fso can't open a file that has a lock for write access, as some old serial programs do. for instance, dos programs.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  14. #14
    Join Date
    Jul 2008
    Location
    Appleton, WI
    Posts
    23

    Re: Reading text file, Please can anyone help

    ah, I used fso completely for my test, opening the file and copying it. That must have been why it was able to copy while the file was open.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured