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.Re adLine 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.ReadLi ne
' Save our position in the file
FilePos += Record.Length
Else
' Move to the start of the end of the last record we read
srReader.BaseSt ream.Seek(FileP os, SeekOrigin.Begi n)
End If
Try
' Read the first record from the file
Record = srReader.ReadLi ne
' 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_FieldSeperato r)
' Load the data into the DataSet object
m_dsFileData.Ta bles("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(Lin eCount)
End If
' Increment the "LineCount" value
LineCount += 1
' Read the next record from the file
Record = srReader.ReadLi ne
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 7 22409
this will increment to read everyline.
--------------------------------------------
Dim filename As StreamReader
filename = File.OpenText(m yTextfile)
Dim instr As String
'read everyline
While filename.Peek > -1
instr = filename.ReadLi ne
Debug.WriteLine (instr)
End While
----------------------------------------------
I would step through(F11) just to ensure its reading every
line.
mg
* "Eric" <an*******@disc ussions.microso ft.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.Re adLine 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.I NI")
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>
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
Hi Eric,
When Record is read in,
Record = srReader.ReadLi ne
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
* "Fergus Cooney" <fi*****@post.c om> 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>
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*******@disc ussions.microso ft.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.Re adLine 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.I NI") 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> .
Fergus,
Thanks, I'll give that a try.
Eric This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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() the string returned by readline() on the file.
The following code snippet works for me:
tokens = "one,two,three,four".Split(",")
for each token in tokens
response.write("<td>"+token+"</td>")
|
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 not know at run time how
many sub objects there may be. Here is an example of the input text file.
0
abiebals,20,50,78
acerrubr,100
|
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 lines I have to read
several times and write them with other values.
What is wrong with this code:
using (StreamWriter swr = new StreamWriter(outPath))
|
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 read. I thought about using
MyStreamReader.BaseStream.Position, but this always seems to return a
multiple of the StreamReader's buffer size (which seems natural-- as I
understand it the StreamReader reads from the underlying stream in discrete...
|
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 typical response in this
protocol
would be:
| |
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);
string sRowText;
while ((sRowText = oStreamFile.ReadLine()) !=null)
|
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 server is on one line (\r\n at end) and
is formed as - each of
which is seperated by a space. Arguments with spaces in them are enclosed in
quotations.
So, I'm able to open a connection to the server. When I send a message to
|
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 have to make the StreamReader jump to some point in the file.
To make the StreamReader jump to where it should be, I use the
following code:
fReader.BaseStream.Seek(fileBookmark + (Me.reportTextWidth *
linesPerPage * startPage),...
|
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 line Count of Sr
}
i just want to get a count of sr.
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |