By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,053 Members | 1,595 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,053 IT Pros & Developers. It's quick & easy.

StreamReader.ReadLine and file position

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
----------------------------------------------
I would step through(F11) just to ensure its reading every
line.

mg

Nov 20 '05 #2

P: n/a
* "Eric" <an*******@discussions.microsoft.com> scripsit:
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()
///

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

P: n/a
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
Nov 20 '05 #4

P: n/a
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
Nov 20 '05 #5

P: n/a
* "Fergus Cooney" <fi*****@post.com> scripsit:
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.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #6

P: n/a
Herfried,
I can read the lines from the file without any issue. The
issue I am having, is saving the "last read" position, so
that on the next read, I can "seek" to that position and
then begin reading lines from the file. It does not
appear to me that "readline" does not update the "current
position" in the streamreader.

Eric
-----Original Message-----
* "Eric" <an*******@discussions.microsoft.com> scripsit:
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()
///

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
.

Nov 20 '05 #7

P: n/a
Fergus,
Thanks, I'll give that a try.

Eric
Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.