473,503 Members | 5,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

StreamReader.ReadLine and file position

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
7 22380
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
* "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
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
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
* "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
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
Fergus,
Thanks, I'll give that a try.

Eric
Nov 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
12213
by: Andy Mee | last post by:
Hello one and all, I'm developing an Asp.NET system to take a CSV file uploaded via the web, parse it, and insert the values into an SQL database. My sticking point comes when I try to split()...
2
5496
by: Bob Cummings | last post by:
Greetings I am stumped. I am trying to create some initial conditions object by reading from a file. These objects have an integer ID and then a list of sub objects. The problem is I will...
3
3224
by: Jaga | last post by:
Hi, how can I read the same passage in a textfile several times? I'm writing a little textgenerator. It reads lines from a file, replaces the variables, and writes it in an other file. Some...
2
12981
by: Keith Kingsley | last post by:
I'm using a StreamReader to read in several lines from an ASCII file. I'd like to know the StreamReader's "true" position-- that is, the number of bytes into the file that the StreamReader has...
3
4982
by: Arno | last post by:
Hi, I'm using TcpClient for communication between two PC running a small piece of software. The protocol used has been designed internally and is HTTP similar (command line, headers, body). A...
1
1509
by: Tarren | last post by:
Hi: Question which might have something to do with encoding? I have a text file and I use the following code to iterate through. StreamReader oStreamFile = new StreamReader(sFileName); ...
21
13024
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
9
5068
by: lord.zoltar | last post by:
Hi, I'm having some trouble with a StreamReader. I use a System.IO.StreamReader to read a text file and then print it. If the user has selected a range of pages starting past the first page, I...
4
17889
by: somequestion | last post by:
Question 1. i am using StreamReader Class like this... string str = string.Empty; StreamReader sr = new StreamReader(fsIn,Encoding.Default) while ((str = sr.ReadLine()) != null) { // wanna get...
0
7063
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7258
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6970
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3146
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
366
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.