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

Thread: StreamReader.BaseStream.Seek Problem

  1. #1
    Join Date
    Sep 2002
    Location
    India (Delhi)
    Posts
    199

    Thumbs down StreamReader.BaseStream.Seek Problem

    Hi all

    in my application ive open a file in the streamreader and pass that streamreader to a function which runs in a loop.
    in that function im changing the position of the cursor to the begining of the file everytime it loops in the functions but problem it move the cursor to first character and then when i try to read a line it skip the first character which makin a problem for me.
    is ther any way for that

    Code:
    Private function SearchSubFunction(sr as streamreader)
    
          sr.BaseStream.Seek(0, SeekOrigin.Begin)
          sr.Read()
    
           While sr.Peek() >= 0
                 single_line = sr.ReadLine.Trim
                 If single_line <> "" AndAlso single_line.StartsWith("'") = False  AndAlso single_line.ToUpper.StartsWith("DIM") = False Then
    
                       'my code goes here
                end if
          end while
    
    End Function
    Keep Posting in this Forum

  2. #2
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,892

    Arrow Re: StreamReader.BaseStream.Seek Problem

    Get rid of that extra sr.Read(). It is causing the cursor to move one character forward.
    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 08/2015 = .NET 4.x and Visual Studio 2015
    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow all threads, so if you have a secondary question, message me.

  3. #3
    Join Date
    Sep 2002
    Location
    India (Delhi)
    Posts
    199

    Re: StreamReader.BaseStream.Seek Problem

    Quote Originally Posted by Craig Gemmill
    Get rid of that extra sr.Read(). It is causing the cursor to move one character forward.
    if i dont use that sr.read() than it wont go on first char its will be at the peek means cursor position will be -1 thats the problem thats why i put that sr.read
    Keep Posting in this Forum

  4. #4
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: StreamReader.BaseStream.Seek Problem

    Code:
    Private function SearchSubFunction(sr as streamreader)
    
          sr.BaseStream.Seek(0, SeekOrigin.Begin)
          sr.DiscardBufferedData()
          'sr.Read()
    
           While sr.Peek() >= 0
                 single_line = sr.ReadLine.Trim
                 If single_line <> "" AndAlso single_line.StartsWith("'") = False  AndAlso single_line.ToUpper.StartsWith("DIM") = False Then
    
                       'my code goes here
                end if
          end while
    
    End Function
    the DiscardBufferedData() clears the buffer so that read methods will fetch the data from the basestream. for best performance in this situation, i would suggest shifting to BinaryStream.
    Busy

  5. #5
    Join Date
    Sep 2002
    Location
    India (Delhi)
    Posts
    199

    Re: StreamReader.BaseStream.Seek Problem

    Quote Originally Posted by Thread1
    Code:
    Private function SearchSubFunction(sr as streamreader)
    
          sr.BaseStream.Seek(0, SeekOrigin.Begin)
          sr.DiscardBufferedData()
          'sr.Read()
    
           While sr.Peek() >= 0
                 single_line = sr.ReadLine.Trim
                 If single_line <> "" AndAlso single_line.StartsWith("'") = False  AndAlso single_line.ToUpper.StartsWith("DIM") = False Then
    
                       'my code goes here
                end if
          end while
    
    End Function
    the DiscardBufferedData() clears the buffer so that read methods will fetch the data from the basestream. for best performance in this situation, i would suggest shifting to BinaryStream.
    Thanx Thread1

    its working cool
    Keep Posting in this Forum

  6. #6
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: StreamReader.BaseStream.Seek Problem

    sure kohlimannu, glad to know that it is working now
    Busy

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)