CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Dec 2010
    Posts
    9

    Reading only new content of *.txt into textbox

    How can I read new contet only, from a *.txt file every sec. File size 0-100mb. This is so I always can have an updatet textbox of the txt. The data must be in lines, same as txt.
    // Thomas

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Reading only new content of *.txt into textbox

    Define "new content" please.

  3. #3
    Join Date
    Dec 2010
    Posts
    9

    Re: Reading only new content of *.txt into textbox

    Using .net 4 Visual Studio Pro.

  4. #4
    Join Date
    Dec 2010
    Posts
    9

    Re: Reading only new content of *.txt into textbox

    The *.txt is getting updated every sec. With new content. The new content comes in lines. Example data:
    [02:30:50] Test data 1
    [02:30:51] Test data 2
    [02:30:52] Test data 3

    I want only the new lines to display in textbox. There may be 100 new lines, and there may be none. Want to check and display every sec.

    Hope this makes it clear

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Reading only new content of *.txt into textbox

    Not really. "New" since when? Who/what is updating the text file?

  6. #6
    Join Date
    Dec 2010
    Posts
    9

    Re: Reading only new content of *.txt into textbox

    The text file is being updated by another software that logs every 1sec, running on the same computer. The new content is new if it wasent there the last time my program (the software I'm developing) checked. It can be from 1sec - 10 hours old.

    txt at 06:00:00
    Hello
    txt at 06:00:01
    Hello you ("you" is now new) - and I want that into my textbox.
    txt at 06:00:02
    Hello you good helper you ("good helper you" is now new) - and I want that into my textbox. And not "you" that aint new anymore

    Hope this helps
    Last edited by Thomas_no; December 15th, 2010 at 09:14 PM.

  7. #7
    Join Date
    Nov 2010
    Posts
    42

    Re: Reading only new content of *.txt into textbox

    You could have a timer event, set it at a specific interval which calls StreamReader and reads the file into the textbox, although you may run into an access error Im not to sure.

  8. #8
    Join Date
    Dec 2010
    Posts
    9

    Re: Reading only new content of *.txt into textbox

    Sounds good. But wont that be very slow on a 50mb txt file, reading it all over again every 1 sec?

  9. #9
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Reading only new content of *.txt into textbox

    You can seek to the last read position in the stream and then read until you are done. Save the new position and around we go.

  10. #10
    Join Date
    Nov 2010
    Posts
    42

    Re: Reading only new content of *.txt into textbox

    Perhaps a timer that checks if the file has been changed, at a very quick quick interval, and if it has been changed grab the last line of the file? That will only work if the software is sending changes 1 at a time. BigEd781's comment seems like a better option though,

  11. #11
    Join Date
    Dec 2010
    Posts
    9

    Re: Reading only new content of *.txt into textbox

    BiGEd: Sounds like the way to go. Could you please give me a sample code? I'm a rookie don't know how to achive that..

    Mastermosley: There may be any number of new lines. (0-500).

    Thank alot for taking time to help!

  12. #12
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: Reading only new content of *.txt into textbox

    Hi Tom.

    I think Ed is correct in that, it seems to me that a lot is riding on exactly how the content changes.

    If it's as trivial as you suggest, that each line is extended by some amount, that's pretty simple ... a single string-function call can handle that. But as the complexity of the change grows, so does the complexity of the change detection and capture.

    for instance,
    t0: Hi there
    t0 + 1 second: Hi there, my name is Bill
    t0 + 2 second" Hi there, my name is Bill .... didn't catch your name.

    those are trivial changes and easily detected and captured.

    but how 'bout more complex changes ?
    t0: Hi There
    t0 + 1 second: Oh my goodness, is it my turn to speak ? Hi There, friend.
    t0 + 2 second: Hi There. Unaccustomed as I am to ... Oh, Hi There, friend. As I was about to say



    yet another problem is the massive amount of data ... that's gonna be one heckuva text box to handle 50MB.

    PERHAPS you can lighten the load by using a listView box running in virtual mode. That way, you might be spared updating all the changes each second by changing only those that lines that are currently displayed in the box. Why waste time and effort changing line 431,207 when the text box is displaying only lines 1...20 ?

    Another problem is synching the two functions: writing the file and updating the textbox. If it's the same process then I guess a simple call would suffice, but if these are two separate processes running, then maybe a pipe (anonymous or named) would help synch the two functions. A system timer would certainly work, but it's been my experience that they tend to drift over time.

    It seems to me that what appears at first to be a simple task is complicated by the scale of the data and the configuration of the process (processes).

    Key to the problem, though, (IMHO) is the complexity of the possible changes.

    But, that's just one Old Man's opinion.... worth exactly what you paid for it.

    bill

  13. #13
    Join Date
    Dec 2010
    Posts
    9

    Re: Reading only new content of *.txt into textbox

    Example from the log:
    [02:02:33] "ANY TEXT 1 "
    [02:02:33] "ANY TEXT 2"
    [02:02:33] "ANY TEXT 3"

    After 1sec. the log may be:
    [02:02:33] "ANY TEXT 1 "
    [02:02:33] "ANY TEXT 2"
    [02:02:33] "ANY TEXT 3"
    [02:02:34] "ANY TEXT 4 "
    [02:02:34] "ANY TEXT 5"
    So the changes is only in the buttom lines of the txt. All lines starts with ["time"]. The text 1-3 in the example will never change, only new data will be added, in new lines.

    When it comes to the textbox containing the entire log, I only need the new lines of data in the textbox.
    Last edited by Thomas_no; December 15th, 2010 at 10:45 PM.

  14. #14
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: Reading only new content of *.txt into textbox

    Ahhhh, that seems to make it VERY much easier. I was concerned about the possibility of very complex changes to an existing 50MB text file. But simply adding new text is (or MAY BE) a snap, at least that's my initial impression.

    At first glance I would think all you'd have to do is
    a: clear the text box text
    b: seek the beginning of the new lines.
    c: read a new line and insert it into the text box
    d: repeat step (c) until EOF is reached.


    You might consider retaining the filesize or offset at the end of one cycle for use in seeking prior to beginning the subsequent cycle.


    I had the impression from reading your earlier post that existing text was subject to change. But that's probably 'cuz I'm an old man and it's way past my bedtime. That's what happens to old men who are subject to confusion in the best of times.

    IMHO, the complexity of the text change is the key to your program's performance. I think you lucked out and were handed the simplest of all possible changes, bro'.

    Best wishes.
    Last edited by ThermoSight; December 16th, 2010 at 12:26 AM.

  15. #15
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Reading only new content of *.txt into textbox

    I would just maybe add a step and a clarification or two.

    1. Seek to the last saved index in the file stream. (Initialize the seek position variable to 0)
    2. Read lines into a StringBuilder until you hit EOF.
    3. Save the current seek position (simply the last index + 1)
    4. Append the string to the TextBox (assuming multi-line TextBox)
    5. Done. The timer can do this every second or so, so repeat.
    Last edited by BigEd781; December 16th, 2010 at 12:55 PM.

Page 1 of 2 12 LastLast

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