473,378 Members | 1,479 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,378 software developers and data experts.

Rewinding a TextReader file stream.

Hi All,
How can i rewind the following sile stream:

TextReader tr = new StreamReader(File.Open(fileName, FileMode.Open));

Is there a dedicated method or shall I close and re-open the file...
Thanks a lot,

Alberto

Oct 16 '06 #1
11 12609
Use a FileStream, which supports random access via the Seek method:

http://msdn2.microsoft.com/en-us/lib...ilestream.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Shooter
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.

<in**@devdept.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
Hi All,
How can i rewind the following sile stream:

TextReader tr = new StreamReader(File.Open(fileName, FileMode.Open));

Is there a dedicated method or shall I close and re-open the file...
Thanks a lot,

Alberto

Oct 16 '06 #2
Kevin Spencer <sp**@uce.govwrote:
Use a FileStream, which supports random access via the Seek method:

http://msdn2.microsoft.com/en-us/lib...ilestream.aspx
But beware that if you seek within the FileStream, you'll need to call
DiscardBufferedData() on the StreamReader, otherwise it'll keep giving
buffered data before reading from the stream again.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 16 '06 #3
Thanks guys,
Can you please provide me a small speudocode example of opening a text
file/reading from it/rewind it/read again?
Thanks,

Alberto

Oct 17 '06 #4
You can see an example in the link I posted.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Shooter
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.

<in**@devdept.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
Thanks guys,
Can you please provide me a small speudocode example of opening a text
file/reading from it/rewind it/read again?
Thanks,

Alberto

Oct 17 '06 #5
Hi Kevin,
I don't see any rewind in that example...
Thanks,

Alberto

Oct 18 '06 #6
in**@devdept.com <in**@devdept.comwrote:
I don't see any rewind in that example...
The example for the Seek method rewinds the stream back to the
beginning.

Personally I prefer to use the Position property, mind you...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 18 '06 #7
Very nice, thanks a lot!

Oct 19 '06 #8
Kevin,

I cannot read a text file line with the FileStream class. The Read
method wants to know before reading how many bytes to read...

I need the StremReader.ReadLine method...

How can I proceed?
Thanks again,

Alberto

Oct 26 '06 #9
I don't know what you mean by "I cannot read a text file line with the
FileStream class." You can't use a method that reads each line, no, but you
can certainly identify line breaks. So, yes, you have to do a bit more work
in terms of your code, breaking the lines yourself.

Here's the deal: If you use a StreamReader/TextReader to read the file, you
can only move forward. To go backward, you would have to close and re-open
the StreamReader. That is going to be time-consuming, in terms of
performance. If you use a FileStream you have random access to the file, but
you have to manage the data you read in your code. The buffer that you read
into is just a temporary storage container, which you can re-use throughout
the lifetime of the FileStream. So, you can identify line breaks and build
your own strings from the data you read.

--
HTH,

Kevin Spencer
Microsoft MVP
Short Order Coder
http://unclechutney.blogspot.com

The devil is in the yada yada yada
<in**@devdept.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Kevin,

I cannot read a text file line with the FileStream class. The Read
method wants to know before reading how many bytes to read...

I need the StremReader.ReadLine method...

How can I proceed?
Thanks again,

Alberto

Oct 26 '06 #10
Thanks a lot Kevin, so I need to write a method that read from the
stream and stop once found a line break and create a string....

I will think about it...

Oct 27 '06 #11
Pretty much, yes. Good luck!

--
HTH,

Kevin Spencer
Microsoft MVP
Short Order Coder
http://unclechutney.blogspot.com

The devil is in the yada yada yada
<in**@devdept.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Thanks a lot Kevin, so I need to write a method that read from the
stream and stop once found a line break and create a string....

I will think about it...

Oct 27 '06 #12

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

Similar topics

6
by: | last post by:
I am rewriting a C++ application in C#. This file has a combination of Text and Binary data. I used CFile before to read the text. If I hit a certain string that denotes the following data is...
11
by: Thomas Matthews | last post by:
Hi, I've searched the FAQ and no tips on this one. Searching the newsgroups shows that the proper method for rewinding an istream is: istream inp; // read past EOF. inp.clear(); // Clear the...
1
by: Colin Green | last post by:
Hi I wonder if anyone has any ideas about this... I am dumping the contents of an XmlDocument into a RichTextBox so the user can see the raw XML. I use a line of code something like this to lado...
2
by: gc | last post by:
I was having trouble getting fgets to read a string from stdin, it was reading a '\n' already in the buffer. Someone told me to rewind stdin before calling fgets. It works, but is it permissible?
3
by: Chan | last post by:
Got in a difficult situation of storing and retrieving TextReader in Cache, but found no such post yet. Tried to store a textreader into Cache object in a similar way illustrated in .NET's SDK...
2
by: Bryan Dickerson | last post by:
StreamReader says it is designed to read a stream of characters StringReader says it is designed to read a string TextReader says it is designed to read a sequential list of characters. I hate...
3
by: Tony Johansson | last post by:
Hello! I just wonder in this specific case is it any advantage to use a TextReader a reference to a StreamReader ? Try { TextReader tr = new StreamReader(locationTextBox.Text); Try {
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.