StreamReader.ReadLine and file position

E

Eric

I am trying to save the "last read" position of a file
using a StreamReader object. I am reading the lines of
the file using StreamReader.ReadLine and then saving the
current position in the file. My problem is that on
subsequent reads, the starting position is not correct.

I have tried many different ways to increment the file
position value, but I cannot get it to work so that on the
next read, the read starts at the correct position.

Any help would be greatly appreciated.

Thanks,

Eric

======================
Currently, my code looks similar to this:
Private Sub ReadLines(ByVal StartingRecord As Int32)
Dim srReader As StreamReader = New StreamReader
(m_Filename)
Dim Record As String
Dim RecordElements() As String
Dim LineCount As Int32
Static FilePos As Long

' Add 1 to our StartingRecord - to account for
the "Header" record
StartingRecord += 1
LineCount = StartingRecord

' Set the value of our "Ending Record" - StartingRecord
+ MaxRecords
EndingRecord = (StartingRecord + m_MaxRecords)

' If this is the first record in the file
If StartingRecord = 2 Then
' Read the record from the file
Record = srReader.ReadLine

' Save our position in the file
FilePos += Record.Length
Else
' Move to the start of the end of the last record we read
srReader.BaseStream.Seek(FilePos, SeekOrigin.Begin)
End If

Try

' Read the first record from the file
Record = srReader.ReadLine

' If this is a record we want
While ((LineCount <= EndingRecord) AndAlso _
(Not (Record Is Nothing)))

' Save our position in the file
FilePos += Record.Length

' Concatenate the record number to the record
Record = Record.Concat((LineCount - 1).ToString & "|",
Record)

' Parse the values from the record
RecordElements = Split(Record, m_FieldSeperator)

' Load the data into the DataSet object
m_dsFileData.Tables("FileData").LoadDataRow
(RecordElements,
True)

' If the line is a multiple of "10" - raise an event
' Event is caught in the main form and used to update
the
' progress bar
If ((LineCount Mod 10) = 0) Then
' Raise the RecordsRead event
RaiseEvent RecordsRead(LineCount)
End If

' Increment the "LineCount" value
LineCount += 1

' Read the next record from the file
Record = srReader.ReadLine
End While

Catch ex As Exception
' Throw the exception back to the caller
Throw ex

Finally
' Close the StreamReader
srReader.Close()

End Try
End Sub
 
M

mannyGonzales

this will increment to read everyline.
--------------------------------------------
Dim filename As StreamReader

filename = File.OpenText(myTextfile)

Dim instr As String

'read everyline
While filename.Peek > -1
instr = filename.ReadLine
Debug.WriteLine(instr)
End While
 
H

Herfried K. Wagner [MVP]

* "Eric said:
I am trying to save the "last read" position of a file
using a StreamReader object. I am reading the lines of
the file using StreamReader.ReadLine and then saving the
current position in the file. My problem is that on
subsequent reads, the starting position is not correct.

I have tried many different ways to increment the file
position value, but I cannot get it to work so that on the
next read, the read starts at the correct position.

Basic code for reading the lines of a file:

\\\
Imports System.IO
..
..
..
Dim sr As New StreamReader("C:\WINDOWS\WIN.INI")
Dim strLine As String
strLine = sr.ReadLine()
Do Until strLine Is Nothing
MsgBox(strLine)
strLine = sr.ReadLine()
Loop
sr.Close()
///
 
F

Fergus Cooney

Hi Herfried,

If you had looked at Eric's code you would have seen that he is quite at
home with reading from a StreamReader. That isn't the issue here.

Regards,
Fergus
 
F

Fergus Cooney

Hi Eric,

When Record is read in,
Record = srReader.ReadLine
it receives all the characters upto, but not including, the end of line.

The file position then moves beyond the end of the line to the start of
the next line.

Here's where the problem is:
FilePos += Record.Length

This adds the number of characters read - but omits the end-of-line
characters. This depends on the system but the default is CrLf for Windows.

So you'll need
FilePos += Record.Length + 2

Regards,
Fergus
 
H

Herfried K. Wagner [MVP]

* "Fergus Cooney said:
If you had looked at Eric's code you would have seen that he is quite at
home with reading from a StreamReader. That isn't the issue here.

After reading the OP's post I thought that he has problems to read a
file line-by-line.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top