CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Best way to read an endless file ?

  1. #1
    Join Date
    Dec 2009

    Best way to read an endless file ?


    i need to read and parse a logfile that is being recorded at all the time and i dont really know what would be the best way to approch and read such a file considering that my main need is to receive the last line first hand as soon as it hit the file.

    So the first thing i was thinking was to use a true while that would read the file even when it hits the null lines and wait for data, that worked wonderfull but then i realised that no one else would be able to access the file until i release it.

    Then i was reading about the FileStream which would prob do what i want, ie:
    FileStream readFile = new FileStream(MyLogFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    Considering that the FileShare would allow me to read it while some one else is doing something with it.

    But as of now i am clueless on how should i really approch it and am looking forward to some help.

    Thanks in advance.

  2. #2
    Join Date
    Mar 2004
    Prague, Czech Republic, EU

    Re: Best way to read an endless file ?

    I would suggest you to look at FileSystemWatcher class and its Changed event.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    May 2007

    Re: Best way to read an endless file ?

    That's definitely how you should open the file. Combining that with a FileSystemWatcher as mentioned above will allow you to only read the file while data is available. The only problem is that it's not particularly safe to read as soon as the event fires as you may get 1 'file changed' event at the very start when the logging application writes 100kB of data. If you read to the end as soon as you get the event is raised you might only get 10kB (as the logging app might not have written all the data yet).

    The best way around that would be to ignore the FSW approach and just call stream.ReadToEnd () every X seconds which will give you any all the most recent data. Just bear in mind that you could still end up 'reading to end' when the logging application is half way through a write, so be prepared for truncated data.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  4. #4
    Join Date
    Dec 2009

    Re: Best way to read an endless file ?


    Currently i've came down to this:
                            StreamReader readFile = new StreamReader(File.Open(MyLogFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
                            while ((ThisLine = readFile.ReadLine()) != null)
                                    string TextBoxData = ThisLine.Replace("\n", "");
                                    Match expgain = Regex.Match(TextBoxData, "You have gained experience in");
                                    if (expgain.Success)
                                        TextBoxData = "G" + ThisLine.Substring(38, ThisLine.Length - 38);
                                        //Calculate(TextBoxData.Substring(19, TextBoxData.Length-19);
                                            iLogFile.Text = TextBoxData + Environment.NewLine + iLogFile.Text;
                                        LastLine = ThisLine;
    but i will now look into FileSystemWatcher and Changed Event... i was thinking on writting the file to a dictionary or listview and use the changed event but it seems that FileSystemWatcher is the way to go for this ...

    Thanks a lot

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)