473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How to create a StreamReader from an existing StreamReader

I need to create a StreamReader which starts from the position of an
existing StreamReader. This way, the second StreamReader can read lines
"ahead" in an own loop, and then I can go back to the outer loop and the
first StreamReader is still where I left it.

I have been trying to do this using this code:

StreamReader sr2 = sr1 //Where sr1 is the existing StreamReader

This seems to work just fine, so I guess this is a supported way of doing
it. The only problem is that when sr2 reaches EOF, sr1 also goes EOF
(EndOfStream). Why is this?

Should I be doing this another way perhaps?

Thanx! /Anders
Nov 16 '05 #1
5 3022
You will have to create two separate StreamReader objects. Otherwise they
share the same BaseStream, and the same position pointer. Compare
sr2.BaseStream.Position and sr1.BaseStream.Position, they are equal.

kevin aubuchon

"Anders Olsson" <ms-publicnews DOT 20 DOT hennish AT spamgourmet DOT com>
wrote in message news:ey**************@TK2MSFTNGP09.phx.gbl...
I need to create a StreamReader which starts from the position of an
existing StreamReader. This way, the second StreamReader can read lines
"ahead" in an own loop, and then I can go back to the outer loop and the
first StreamReader is still where I left it.

I have been trying to do this using this code:

StreamReader sr2 = sr1 //Where sr1 is the existing StreamReader

This seems to work just fine, so I guess this is a supported way of doing
it. The only problem is that when sr2 reaches EOF, sr1 also goes EOF
(EndOfStream). Why is this?

Should I be doing this another way perhaps?

Thanx! /Anders

Nov 16 '05 #2
<"Anders Olsson" <ms-publicnews DOT 20 DOT hennish AT spamgourmet DOT
com>> wrote:
I need to create a StreamReader which starts from the position of an
existing StreamReader. This way, the second StreamReader can read lines
"ahead" in an own loop, and then I can go back to the outer loop and the
first StreamReader is still where I left it.

I have been trying to do this using this code:

StreamReader sr2 = sr1 //Where sr1 is the existing StreamReader

This seems to work just fine, so I guess this is a supported way of doing
it. The only problem is that when sr2 reaches EOF, sr1 also goes EOF
(EndOfStream). Why is this?
StreamReader is a reference type. Your line above just sets the value
of sr2 to the value of sr1 - both references to the same object. If
this doesn't immediately make it clear, see
http://www.pobox.com/~skeet/csharp/parameters.html which has a brief
explanation of reference types. (Must write a full article about that
some time...)
Should I be doing this another way perhaps?


Definitely. You'll probably need to write your own buffering reader
though - either that or seek back to the original place in the base
stream and call DiscardBufferedData (assuming your stream supports
seeking).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
> You will have to create two separate StreamReader objects. Otherwise they
share the same BaseStream, and the same position pointer. Compare
sr2.BaseStream.Position and sr1.BaseStream.Position, they are equal.


Ok, but it seems I can't have two different StreamReaders reading the same
file at the same time. :(
Nov 16 '05 #4
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
http://www.pobox.com/~skeet/csharp/parameters.html which has a brief
explanation of reference types. (Must write a full article about that
some time...)


Hi. Thanks for the explanation. I understand now, that I need two different
StreamReaders (or is it Streams?). Anyway, I don't think this is possible
since the source data is a file. If I try to create a second Stream and
StreamReader, i get a "file in use" exception. (Also see my reply to Kevin's
post).
Should I be doing this another way perhaps?


Definitely. You'll probably need to write your own buffering reader
though - either that or seek back to the original place in the base
stream and call DiscardBufferedData (assuming your stream supports
seeking).


Wow! Writing my own buffering reader sounds a little too complicated for me.
:)

Is there a way to "save" the position of a StreamReader and seek back to it
at after reading a couple of lines ahead? How?

Or would I have to save the current line as a string, close the stream,
reopen it, and seek forward until I get to the same string where I left off?
Sounds like a bad way to to it, performance wise, although the log files are
"only" 3-4 megs in size.

Any other ideas on how to do this?

/Anders
Nov 16 '05 #5
<"Anders Olsson" <ms-publicnews DOT 20 DOT hennish AT spamgourmet DOT
com>> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
http://www.pobox.com/~skeet/csharp/parameters.html which has a brief
explanation of reference types. (Must write a full article about that
some time...)
Hi. Thanks for the explanation. I understand now, that I need two different
StreamReaders (or is it Streams?). Anyway, I don't think this is possible
since the source data is a file. If I try to create a second Stream and
StreamReader, i get a "file in use" exception. (Also see my reply to Kevin's
post).


You should be able to open the file twice if you open it just for
reading, and with file sharing.
Should I be doing this another way perhaps?


Definitely. You'll probably need to write your own buffering reader
though - either that or seek back to the original place in the base
stream and call DiscardBufferedData (assuming your stream supports
seeking).


Wow! Writing my own buffering reader sounds a little too complicated for me.
:)

Is there a way to "save" the position of a StreamReader and seek back to it
at after reading a couple of lines ahead? How?


Well, you can use BaseStream.Position, but then you'll be slightly out
due to buffering :(
Or would I have to save the current line as a string, close the stream,
reopen it, and seek forward until I get to the same string where I left off?
Sounds like a bad way to to it, performance wise, although the log files are
"only" 3-4 megs in size.

Any other ideas on how to do this?


I think opening the file twice is the best bet. Use FileAccess.Read and
FileShare.Read when opening the file, and you should be okay.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

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

Similar topics

11
by: Brian W. Smith | last post by:
I have XML Documents that I am digitally signing, and need to be able to load these documents into DataSet objects. However, after checking the signature and using the DataSet.ReadXML method, I am...
4
by: Just Me | last post by:
I need to get a StreamReader Dim lsr0 As New StreamReader(FullPath) If the file does not exist I'd like it to create one. Do I have to Try/Catch or is there a clean way of doing it?
3
by: MattB | last post by:
Hi. I'm going around and around with an issue that I can't seem to get around. I have a function I wrote that uses a StreamReader to read a text file into a string variable. It's been working well...
1
by: foreman | last post by:
Hi there, Hello everybody. I am a newbie to dot net framework class lib. I am confused about those classes such as all of the stream classes and those XXXReader XXXWriter. In fact, I have tried...
16
by: danielbuus | last post by:
....or, to put it perhaps more communicative, something like this: Type someObjectsType = someObject.GetType(); someObjectsType newObject = new someObjectsType(); Is this possible? If so, how?...
0
by: JR | last post by:
Hello All - Here's the deal. I got a .net windows service that reads files from a unc share and writes them to another shared folder on the same box. After about 24 hours we get "network name is...
4
by: moondaddy | last post by:
I need to edit the text in many files so I'm writing a small routine to do this. First I have a method that loops through all the files in a directory and passes the full file path to another...
1
by: ujjwaltrivedi | last post by:
Hey guys, Can anyone tell me how to create a text file with Unicode Encoding. In am using FileStream Finalfile = new FileStream("finalfile.txt", FileMode.Append, FileAccess.Write); ...
0
by: yrogirg | last post by:
Actually, I need utf-8 to utf-8 encoding which would change the text to another keyboard layout (e.g. from english to russian ghbdtn -> привет) and would not affect other symbols. I`m totally...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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,...
0
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
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
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...

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.