473,804 Members | 3,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rewinding a TextReader file stream.

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

TextReader tr = new StreamReader(Fi le.Open(fileNam e, 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 12660
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.c omwrote in message
news:11******** *************@b 28g2000cwb.goog legroups.com...
Hi All,
How can i rewind the following sile stream:

TextReader tr = new StreamReader(Fi le.Open(fileNam e, 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.govwr ote:
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
DiscardBuffered Data() on the StreamReader, otherwise it'll keep giving
buffered data before reading from the stream again.

--
Jon Skeet - <sk***@pobox.co m>
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.c omwrote in message
news:11******** *************@e 3g2000cwe.googl egroups.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.co m <in**@devdept.c omwrote:
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.co m>
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.Rea dLine 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.c omwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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.Rea dLine method...

How can I proceed?
Thanks again,

Alberto

Oct 26 '06 #10

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

Similar topics

6
519
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 binary, I used the current position in the file and another stream to read to the binary data. All text data is ended with a carriage return / line feed while the binary is actually an image file listed byte by byte. Preceding the binary data...
11
3497
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 stream state inp.seekg(0);
1
2555
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 the Xml text into the textbox: richTextBox.Text = xmlDoc.ToString(); I recon this technique is inefficient with large xml documents.
2
10238
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
1459
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 doc ("Retrieving Values of Cached Items"): ' ----------------------------- ... Dim CPCache As textreader = CType(Cache("CP"), textreader) If CPCache Is Nothing Then ...
2
16309
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 to sound like a VB6 grump, but aren't we splitting hairs?? What's the difference? -- TFWBWY...A
3
3732
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
9587
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,...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
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,...
0
10085
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7623
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 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...
0
5527
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...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2998
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 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...

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.