I am reading a text file line by line using StreamReader.Readline(). After reading a line i have to check whether some fields of the line which is read are present in the rest of the file or not.

What i am doing is that after reading a line from the file i am passing the streamreader to check whether there are multiple entries. i am trying to use line.Length() to get the length of line, so using this length i can go back to the line
--------------------------------------------------------------------------------------
my code:

Dim pos as Long

   sr = File.OpenText(filepath)

   While sr.Peek() <> -1

        Dim lineRead As String = sr.ReadLine()

        Dim res As Boolean = CheckIfMultipleEntries(sr)

        pos = -pos

        If res Then

           // save data from those lines and skip these lines

         p += lineRead.Length()
       
       End If

     pos = pos + p   // go to the line which is after the lines skiped
     sr.BaseStream.Seek(pos, SeekOrigin.Current)

  End While


Function CheckIfMultipleEntries(ByVal sr1 As StreamReader) As Boolean

    pos = 0

    While sr1.Peek() <> -1

        Dim l As String = sr1.ReadLine()

        //check if line is present in the rest of the file

        pos += l.Length()

    End While


 End Function

-------------------------------------------------------------------------------------

my problem is that it is not giving me the expected line or it is skipping some part of the line.
any suggestions? how can i return back to my line?

Dani AI

Generated

Your code is failing because StreamReader.ReadLine() returns characters (without the newline) while BaseStream.Seek works in bytes — and StreamReader also buffers data. Using line.Length (characters) as a byte offset will therefore often land you in the wrong place (missing newline bytes, multi-byte encodings like UTF‑8, and the reader's internal buffer all make the math unreliable). is on the right track to try to track position, and 's pointer to ReadLine docs is fine, but the real issue is mixing character counts with byte positions and StreamReader buffering.

A simple, robust solution (when the file fits in memory) is to read all lines first and process by index so you can skip ahead deterministically:

Dim lines = File.ReadAllLines(filepath)
Dim i As Integer = 0
While i < lines.Length
    Dim current = lines(i)
    Dim key = GetKeyFromLine(current) ' parse the fields you care about
    Dim j = i + 1
    While j < lines.Length AndAlso GetKeyFromLine(lines(j)) = key
        j += 1
    End While
    ' process the block lines(i) .. lines(j-1)
    i = j
End While

For very large files, do a single-pass grouping (write grouped entries to a temp file or use a Dictionary of small keys pointing to file offsets), or perform two passes: first pass build an index of keys and offsets, second pass process groups. If you must reposition a StreamReader, do not compute offsets from String.Length. Instead record the BaseStream.Position before reading, and if you Seek you must call sr.DiscardBufferedData() after changing BaseStream.Position to resync the StreamReader. Even then, computing offsets yourself is fragile unless you control file encoding and newline format — it can break on multi-byte characters or unexpected line endings.

maybe its not possible,
maybe this will help u :

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.