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
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