Read from stream

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • csharpula csharp

    #1

    Read from stream


    Hello,
    I would like to know how can I read a file by this way:
    Read from line X to Y and in other iteration from line Y to P and on and
    on. How to implement it? Which method and how? Thank u!


    *** Sent via Developersdex http://www.developersdex.com ***
  • Jon Skeet [C# MVP]

    #2
    Re: Read from stream

    On Dec 19, 7:58 am, csharpula csharp <csharp...@yaho o.comwrote:
    I would like to know how can I read a file by this way:
    Read from line X to Y and in other iteration from line Y to P and on and
    on. How to implement it? Which method and how? Thank u!
    It's not clear what you mean - what are X, Y and P? Could you describe
    the issue you're trying to solve in real-world terms?

    Jon

    Comment

    • Marc Gravell

      #3
      Re: Read from stream

      If you are talking about "lines", then you probably want a
      StreamReader, via File.OpenText(p ath) - which has methods such as
      ReadLine(). If you are just reading forwards, then this should be
      quite easy - just keep track of which line you are on. You could
      probably wrap it up a bit for convenience, but it is hard to say "how"
      without a bit more information on what you are trying to do...

      Marc

      Comment

      • csharpula csharp

        #4
        Re: Read from stream


        I will try to explain myself :I want to read couple of times same file
        which is being updated all the time. I want to read from beggining till
        end at phase A and in phaseB to read from the end of A till the new end
        and so on.
        How to do it? Hope it's more clear. Thanks!


        *** Sent via Developersdex http://www.developersdex.com ***

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Read from stream

          csharpula csharp <csharpula@yaho o.comwrote:
          I will try to explain myself :I want to read couple of times same file
          which is being updated all the time. I want to read from beggining till
          end at phase A and in phaseB to read from the end of A till the new end
          and so on.
          How to do it? Hope it's more clear. Thanks!
          Well, you can find the position of a stream, then reopen the stream and
          seek directly to that position, if that helps.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          World class .NET training in the UK: http://iterativetraining.co.uk

          Comment

          • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

            #6
            Re: Read from stream



            "Jon Skeet [C# MVP]" wrote:
            csharpula csharp <csharpula@yaho o.comwrote:
            I will try to explain myself :I want to read couple of times same file
            which is being updated all the time. I want to read from beggining till
            end at phase A and in phaseB to read from the end of A till the new end
            and so on.
            How to do it? Hope it's more clear. Thanks!
            >
            Well, you can find the position of a stream, then reopen the stream and
            seek directly to that position, if that helps.
            >
            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            World class .NET training in the UK: http://iterativetraining.co.uk
            >
            .... and hope that the buffer is flushed on complete lines by the process
            that is writing the file. It could get tricky otherwise.

            Comment

            • christery@gmail.com

              #7
              Re: Read from stream

              ... and hope that the buffer is flushed on complete lines by the process
              that is writing the file. It could get tricky otherwise.
              and that one can read a file opend by another process... got that
              problem switching from os/2 file servers to nt4 back in the days... NT
              ans OS/2 share was read only for the reader but the file was locked in
              nt, not in os/2, and the program doing it was bought, some old
              datalogger, AAC-2 I think...

              havent tried that since then in win, but got the feeling somehow that
              an app like word for example locks files "harder" than notepad... not
              investigated that, so it might be a problem if he cant control the
              source of the file... in vms u can do a backup/ignore=interloc k but
              the problem with flushing the buffer (as Jon said) is still there.

              can you get around that with storing data in a DB instead? then you
              can use triggers and other methods (Sp to insert data only stores new
              data in other table, when you read it clean it) to tell when new data
              arrives too...

              thats just ideas... good luck..

              //CY

              Comment

              • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                #8
                Re: Read from stream

                csharpula csharp wrote:
                I will try to explain myself :I want to read couple of times same file
                which is being updated all the time. I want to read from beggining till
                end at phase A and in phaseB to read from the end of A till the new end
                and so on.
                How to do it?
                Some combination of Open/Seek/Close.

                Maybe the little hack attached below can get you started.

                Arne

                =============== =============== =============== ===

                using System;
                using System.IO;
                using System.Threadin g;

                namespace E
                {
                public class TailSniff
                {
                private string filename;
                private long pos;
                public TailSniff(strin g filename)
                {
                this.filename = filename;
                pos = 0;
                }
                public StreamReader Read()
                {
                FileStream fs = new FileStream(file name, FileMode.Open,
                FileAccess.Read );
                long len = fs.Length;
                fs.Seek(pos, SeekOrigin.Begi n);
                byte[] b = new byte[len - pos];
                fs.Read(b, 0, b.Length);
                fs.Close();
                pos = len;
                return new StreamReader(ne w MemoryStream(b) );
                }
                }
                public class MainClass
                {
                public static void Main(string[] args)
                {
                TailSniff ts = new TailSniff(@"C:\ z.z");
                for(;;)
                {
                StreamReader sr = ts.Read();
                string line;
                while((line = sr.ReadLine()) != null)
                {
                Console.WriteLi ne(line);
                }
                sr.Close();
                Thread.Sleep(10 );
                }
                }
                }
                }

                Comment

                • csharpula csharp

                  #9
                  Re: Read from stream



                  Hello,
                  The problem is that I can't use Seek or strea.lenth in StreamReader. So
                  how can I do it? Thanks!

                  *** Sent via Developersdex http://www.developersdex.com ***

                  Comment

                  • Marc Gravell

                    #10
                    Re: Read from stream

                    I can't use Seek or strea.lenth in StreamReader

                    You can seek the .BaseStream and call .DiscardBuffere dData(), though

                    Marc

                    Comment

                    • csharpula csharp

                      #11
                      Re: Read from stream


                      But this stream is reading stream and it throwsexception in debug if I
                      use Seek with Stream. Any other ideas? Thansk a lot!


                      *** Sent via Developersdex http://www.developersdex.com ***

                      Comment

                      • RobertK

                        #12
                        Re: Read from stream

                        You might try something like this:

                        static FileStream FS;
                        static StreamReader SR;
                        static int CharsRead;
                        static char[] buff;

                        public static void GetText()
                        {
                        int count = 0;
                        if (CharsRead 1) SR.BaseStream.P osition = CharsRead - 1;
                        do
                        {
                        count = SR.ReadBlock(bu ff, 0, 2048);
                        if (count 0)
                        {
                        CharsRead += count;
                        string st = new string(buff);
                        if (count < 2048) st = st.Substring(0, count);
                        Console.WriteLi ne(st);
                        }
                        } while (SR.Peek() != -1);
                        }

                        static void Main(string[] args)
                        {
                        buff = new char[2048];
                        FS = new FileStream("D:\ \testtext.txt", FileMode.Open,
                        FileAccess.Read , FileShare.ReadW rite);
                        SR = new StreamReader(FS );
                        GetText();
                        Console.WriteLi ne ("Type 'quit' and ENTER to quit");
                        while (Console.ReadLi ne() != "quit")
                        {
                        GetText();
                        Console.WriteLi ne("Type 'quit' and ENTER to quit");
                        }
                        SR.Close();

                        Comment

                        Working...