Tracking the position of the line of a streamreader

后端 未结 4 1663
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 11:11

Hi guys what i need to do is track the position of the line that I am reading from the stream reader when I say reader.ReadLine() i need to know the position of

4条回答
  •  甜味超标
    2020-12-03 11:44

    Maybe this can help you

      public class StreamLineReader : IDisposable
        {
            const int BufferLength = 1024;
    
            Stream _Base;
            int _Read = 0, _Index = 0;
            byte[] _Bff = new byte[BufferLength];
    
            long _CurrentPosition = 0;
            int _CurrentLine = 0;
    
            /// 
            /// CurrentLine number
            /// 
            public long CurrentPosition { get { return _CurrentPosition; } }
            /// 
            /// CurrentLine number
            /// 
            public int CurrentLine { get { return _CurrentLine; } }
            /// 
            /// Constructor
            /// 
            /// Stream
            public StreamLineReader(Stream stream) { _Base = stream; }
            /// 
            /// Count lines and goto line number
            /// 
            /// Goto Line number
            /// Return true if goTo sucessfully
            public bool GoToLine(int goToLine) { return IGetCount(goToLine, true) == goToLine; }
            /// 
            /// Count lines and goto line number
            /// 
            /// Goto Line number
            /// Return the Count of lines
            public int GetCount(int goToLine) { return IGetCount(goToLine, false); }
            /// 
            /// Internal method for goto&Count
            /// 
            /// Goto Line number
            /// Stop when found the selected line number
            /// Return the Count of lines
            int IGetCount(int goToLine, bool stopWhenLine)
            {
                _Base.Seek(0, SeekOrigin.Begin);
                _CurrentPosition = 0;
                _CurrentLine = 0;
                _Index = 0;
                _Read = 0;
    
                long savePosition = _Base.Length;
    
                do
                {
                    if (_CurrentLine == goToLine)
                    {
                        savePosition = _CurrentPosition;
                        if (stopWhenLine) return _CurrentLine;
                    }
                }
                while (ReadLine() != null);
    
                // GoToPosition
    
                int count = _CurrentLine;
    
                _CurrentLine = goToLine;
                _Base.Seek(savePosition, SeekOrigin.Begin);
    
                return count;
            }
            /// 
            /// Read Line
            /// 
            /// 
            public string ReadLine()
            {
                bool found = false;
    
                StringBuilder sb = new StringBuilder();
                while (!found)
                {
                    if (_Read <= 0)
                    {
                        // Read next block
                        _Index = 0;
                        _Read = _Base.Read(_Bff, 0, BufferLength);
                        if (_Read == 0)
                        {
                            if (sb.Length > 0) break;
                            return null;
                        }
                    }
    
                    for (int max = _Index + _Read; _Index < max; )
                    {
                        char ch = (char)_Bff[_Index];
                        _Read--; _Index++;
                        _CurrentPosition++;
    
                        if (ch == '\0' || ch == '\n')
                        {
                            found = true;
                            break;
                        }
                        else if (ch == '\r') continue;
                        else sb.Append(ch);
                    }
                }
    
                _CurrentLine++;
                return sb.ToString();
            }
            /// 
            /// Free resources
            /// 
            public void Dispose()
            {
                if (_Base != null)
                {
                    _Base.Close();
                    _Base.Dispose();
                    _Base = null;
                }
            }
        }
    

    Use:

     using (StreamLineReader st = new StreamLineReader(File.OpenRead("E:\\log.txt")))
            {
                bool ok = st.GoToLine(1);
                int count= st.GetCount(0);
    
                string w0 = st.ReadLine();
                string w1 = st.ReadLine();
                string w2 = st.ReadLine();
                string w3 = st.ReadLine();
            }
    

提交回复
热议问题