CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2008
    Location
    Kentucky
    Posts
    73

    Infinite Loop text file read

    What I'm trying to do is create an app that will open a text file (that is currently being appended to by another program), read the newest lines, pull information from those lines into various variables, and then wait until more lines are added.

    I've got the opening of the file and the variable comparisons working fine... what I can't get is how to make it read only the lines from the time my app is started until I close it and pull the information from those lines.

    I'm using a StreamReader... and my code (plus attempted codes, yes it's ugly at the moment) looks like:

    Code:
                //while ((strLine = sr.ReadLine()) != null)
                //buffersize = 4096;
                //char[] buffer = new char[buffersize];
    
                //while (1 != 2)
                //{
                    fs = new FileStream(strPath2, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    sr = new StreamReader(fs);
                    //sr.BaseStream.Seek(intLength1, SeekOrigin.Current);
                    intLength2 = Convert.ToInt32(sr.BaseStream.Length);
                    while (intLength2 > intLength1)
                    {
                        long intStore = Convert.ToInt64(intLength2 - intLength1);
                        buffersize = intLength2;
                        char[] buffer = new char[buffersize];
                        //sr.BaseStream.Seek(intStore, SeekOrigin.Begin);
                        
                        //sr.BaseStream.Position = intLength1;
                        intLength1 = Convert.ToInt32(sr.BaseStream.Length);
                        sr.BaseStream.Seek(intStore, SeekOrigin.End);
                        strLine = Convert.ToString(sr.Read(buffer, intLength1, Convert.ToInt32(intStore)));
                        //sr.BaseStream.SetLength(Convert.ToInt64(intStore));
                        while ((strLine = sr.ReadLine()) != null)
                        //the rest of my code
    Anyone have any tips on how to make this work?

  2. #2
    Join Date
    Jul 2007
    Location
    Manila, Philippines
    Posts
    6

    Re: Infinite Loop text file read

    add indicator in your text file (for example line number), then that will be your reference in getting only the lines from the time your application started until you closed it.

  3. #3
    Join Date
    Apr 2008
    Location
    Kentucky
    Posts
    73

    Re: Infinite Loop text file read

    Quote Originally Posted by ALdeC
    add indicator in your text file (for example line number), then that will be your reference in getting only the lines from the time your application started until you closed it.
    If you're saying to add a line number to the actual text file, that won't work as it is constantly being modified by another application.

    I was given the idea of using the file size to get my starting point and telling my app when to start the read, however I can't seem to get that to work correctly. I can get it to kind of work by reading the lines to an array and comparing each element... but it take 30-50% of the CPU and seems to miss quite a few lines.

    Here's what I have that "kind of" works:

    Code:
    fs = new FileStream(strPath2, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    sr = new StreamReader(fs);
    strLength1 = Convert.ToString(sr.BaseStream.Length);
    fs.Close();
    while (1 != 2)
    {
           fs = new FileStream(strPath2, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
           sr = new StreamReader(fs);
           strLength2 = Convert.ToString(sr.BaseStream.Length);
           while (Convert.ToInt32(strLength2) > Convert.ToInt32(strLength1))
           {
                  long lngStore = Convert.ToInt64(strLength2) - Convert.ToInt64(strLength1);
                  sr.BaseStream.Seek(-lngStore, SeekOrigin.End);
                  sr.BaseStream.Position = Convert.ToInt64(strLength1);
                  strLine = sr.ReadToEnd();
                  Array aLines = strLine.Split('\r');
                  strLength1 = Convert.ToString(sr.BaseStream.Length);
                  //The rest of my code
    Any better ideas?
    Last edited by yraen; April 15th, 2008 at 01:30 PM.

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Infinite Loop text file read

    Copy the file to another file, and then read that.

    For instance, if the copied file is called 'copy.txt'.

    (1) Read the size of 'copy.txt'.
    (2) Copy the constantly-being-written-to file to 'copy.txt', overwriting it.
    (3) Start reading 'copy.txt' after the end of the original 'copy.txt'.

    Get the idea ?

    This'll also stop the file from changing beneath your feet when you're reading it.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  5. #5
    Join Date
    Apr 2008
    Location
    Kentucky
    Posts
    73

    Re: Infinite Loop text file read

    Quote Originally Posted by darwen
    Copy the file to another file, and then read that.

    For instance, if the copied file is called 'copy.txt'.

    (1) Read the size of 'copy.txt'.
    (2) Copy the constantly-being-written-to file to 'copy.txt', overwriting it.
    (3) Start reading 'copy.txt' after the end of the original 'copy.txt'.

    Get the idea ?

    This'll also stop the file from changing beneath your feet when you're reading it.

    Darwen.
    Sounds plausible, just wondering how efficient it will be as some of the text files can be 100's of mb in size...

    I'll give it a try and see how it goes though, thanks
    Last edited by yraen; April 15th, 2008 at 03:26 PM.

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