473,394 Members | 2,063 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,394 software developers and data experts.

how to use seek() in StreamReader class?

We can use seek() in the FileStream class,as we know.
But I found that seek() is not work correctly in StreamReader.
Who can tell me how to use seek() correctly in StreamReader?
thanks a lot!
I use the seek() like this:
StreamReader r;
.......
r.BaseStream.seek(.....);

Nov 16 '05 #1
11 35657
Tiger,

Try calling r.DiscardBufferedData() to clear the stream's buffer of data.
If you continue to have problems please post your code and exactly what you
want to accomplish.

Hope this helps.
Nov 16 '05 #2
Brian Brown,
My code is like this:
FileStream fs;
fs=new FileStream("cc.txt",FileMode.Open,FileAccess.Read) ;
StreamReader r=new StreamReader(fs);
Console.WriteLine((char)r.Peek());
//r.DiscardBufferedData();
r.BaseStream.Seek(5,SeekOrigin.Current);
//r.DiscardBufferedData();
Console.WriteLine((char)r.Peek());
The content of cc.txt is :abcdefghijklmnop
I thought the result which is printed on sreen is:
a
f
But in fact is
a
a
why?
I have tried r.DiscardBufferedData(),which is remarked in my code,but the
result is
a
?
I hope your reply,thanks a lot!
"Brian Brown" wrote:
Tiger,

Try calling r.DiscardBufferedData() to clear the stream's buffer of data.
If you continue to have problems please post your code and exactly what you
want to accomplish.

Hope this helps.

Nov 16 '05 #3
Tiger,

I am glad that you posted your code. From the earlier posting I was
thinking that you had data left over in the buffer. Now I think that I see
what you are trying to accomplish. I will attempt to explain…

Seek is a method of the BaseStream (i.e. FileStream). If you call Seek(5,
SeekOrigin.Current) on the BaseStream (remember this is the FileStream not
the StreamReader…) you have moved the file pointer forward 5 places. If you
call the BaseStream.Read method you will see that the returned value is
offset 5 places from where the pointer used to be pointing.

The StreamReader has no such method as Seek. Since this is the case you will
not get the results you expect when you call StreamReader.Peek(). The Peek()
method uses a different pointer to the data. It will see the next character
in the stream according to where the StreamReader is pointing not where the
BaseStream pointer is located. I have posted some sample code below to
demonstrate my point.

I hope this helps

---------------
using System;
using System.IO;
using System.Text;

..
..
..
const String FILENAME = @"D:\cc.txt";

FileStream fs;
fs=new FileStream(FILENAME,FileMode.Open,FileAccess.Read) ;
StreamReader r=new StreamReader(fs);
Byte[] myByteArray = new Byte[1];
ASCIIEncoding myEncoding = new ASCIIEncoding();

//call seek on the base stream (i.e. the filestream)
r.BaseStream.Seek(0,SeekOrigin.Begin);
//read the first character from the file stream
r.BaseStream.Read(myByteArray,0,1);
//output to the console
Console.WriteLine(myEncoding.GetString(myByteArray ));

//call seek on the base stream and move it up 5 places
r.BaseStream.Seek(5,SeekOrigin.Current);
//read the first character from the file stream
r.BaseStream.Read(myByteArray,0,1);
//output to the console
Console.WriteLine(myEncoding.GetString(myByteArray ));

//close the streams
r.Close();
fs.Close();

Nov 16 '05 #4
Tiger <Ti***@discussions.microsoft.com> wrote:
Brian Brown,
My code is like this:
FileStream fs;
fs=new FileStream("cc.txt",FileMode.Open,FileAccess.Read) ;
StreamReader r=new StreamReader(fs);
Console.WriteLine((char)r.Peek());
//r.DiscardBufferedData();
r.BaseStream.Seek(5,SeekOrigin.Current);
//r.DiscardBufferedData();
Console.WriteLine((char)r.Peek());


<snip>

Just to explain in a slightly different way to Brian (maybe).

When you first call r.Peek(), the StreamReader is reading a chunk of
data from the stream. That leaves the stream positioned later in the
data than you'd expect - at the end of the file, in your particular
case. You're then telling the stream to reposition itself 5 bytes later
- i.e. still at the end of the file. You're then telling the
StreamReader to discard the buffered data (assuming you've removed the
comment), and it then tries to read from the stream, and finds out it's
at the end of the file.

So, if you change your SeekOrigin.Current to SeekOrigin.Begin you'll
end up with

a
f

instead. It does mean you need to keep track of where you want to go to
rather than using SeekOrigin.Current, unfortunately - if you just want
to skip five characters, then using StreamReader.Read (remembering to
use the return value to find out how many characters you've actually
read) is probably easier.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Thanks a lot,Brian Brown and Jon Skeet!
So there's no way to use r.BaseStream.seek() cerrectly?
I used the seek() in another app like this
StreamReader r=new StreamReader(filename);
....
r.BaseStream.seek();
I hope to move file pointer,so seek() method is not fit here.
Could you tell me any other way to move file pointer in StreamReader?

"Brian Brown" wrote:
Tiger,

I am glad that you posted your code. From the earlier posting I was
thinking that you had data left over in the buffer. Now I think that I see
what you are trying to accomplish. I will attempt to explain…

Seek is a method of the BaseStream (i.e. FileStream). If you call Seek(5,
SeekOrigin.Current) on the BaseStream (remember this is the FileStream not
the StreamReader…) you have moved the file pointer forward 5 places. If you
call the BaseStream.Read method you will see that the returned value is
offset 5 places from where the pointer used to be pointing.

The StreamReader has no such method as Seek. Since this is the case you will
not get the results you expect when you call StreamReader.Peek(). The Peek()
method uses a different pointer to the data. It will see the next character
in the stream according to where the StreamReader is pointing not where the
BaseStream pointer is located. I have posted some sample code below to
demonstrate my point.

I hope this helps

---------------
using System;
using System.IO;
using System.Text;

.
.
.
const String FILENAME = @"D:\cc.txt";

FileStream fs;
fs=new FileStream(FILENAME,FileMode.Open,FileAccess.Read) ;
StreamReader r=new StreamReader(fs);
Byte[] myByteArray = new Byte[1];
ASCIIEncoding myEncoding = new ASCIIEncoding();

//call seek on the base stream (i.e. the filestream)
r.BaseStream.Seek(0,SeekOrigin.Begin);
//read the first character from the file stream
r.BaseStream.Read(myByteArray,0,1);
//output to the console
Console.WriteLine(myEncoding.GetString(myByteArray ));

//call seek on the base stream and move it up 5 places
r.BaseStream.Seek(5,SeekOrigin.Current);
//read the first character from the file stream
r.BaseStream.Read(myByteArray,0,1);
//output to the console
Console.WriteLine(myEncoding.GetString(myByteArray ));

//close the streams
r.Close();
fs.Close();

Nov 16 '05 #6
Tiger <Ti***@discussions.microsoft.com> wrote:
Thanks a lot,Brian Brown and Jon Skeet!
So there's no way to use r.BaseStream.seek() cerrectly?
There is, but using it with SeekOrigin.Current will give unexpected
results.
I used the seek() in another app like this
StreamReader r=new StreamReader(filename);
...
r.BaseStream.seek();
I hope to move file pointer,so seek() method is not fit here.
Could you tell me any other way to move file pointer in StreamReader?


Seeking is fine, so long as you know where you want to go to relative
to the start or end of the file.

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

"Jon Skeet [C# MVP]" wrote:
Tiger <Ti***@discussions.microsoft.com> wrote:
Thanks a lot,Brian Brown and Jon Skeet!
So there's no way to use r.BaseStream.seek() cerrectly?


There is, but using it with SeekOrigin.Current will give unexpected
results.
I used the seek() in another app like this
StreamReader r=new StreamReader(filename);
...
r.BaseStream.seek();
I hope to move file pointer,so seek() method is not fit here.
Could you tell me any other way to move file pointer in StreamReader?


Seeking is fine, so long as you know where you want to go to relative
to the start or end of the file.

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

Nov 16 '05 #8
Tiger <Ti***@discussions.microsoft.com> wrote:
Could you give me a example?


As I said before, just change your code (including calling
DiscardBufferedData after the seek) to use

r.BaseStream.Seek(5, SeekOrigin.Begin)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
I know!
Do you mean if I want to set the file pointer,I must remember the number of
the characters which have been read???
And I must use SeekOrigin.Begin?

"Jon Skeet [C# MVP]" wrote:
Tiger <Ti***@discussions.microsoft.com> wrote:
Could you give me a example?


As I said before, just change your code (including calling
DiscardBufferedData after the seek) to use

r.BaseStream.Seek(5, SeekOrigin.Begin)

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

Nov 16 '05 #10
Tiger <Ti***@discussions.microsoft.com> wrote:
I know!
Do you mean if I want to set the file pointer,I must remember the number of
the characters which have been read???
And I must use SeekOrigin.Begin?


Potentially - and it gets harder if you have a multibyte character
encoding, too.

However, if you just want to skip a few characters, I'd use Read if I
were you.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11
Thank you very much,Jon!
you helped me a lot!

"Jon Skeet [C# MVP]" wrote:
Tiger <Ti***@discussions.microsoft.com> wrote:
I know!
Do you mean if I want to set the file pointer,I must remember the number of
the characters which have been read???
And I must use SeekOrigin.Begin?


Potentially - and it gets harder if you have a multibyte character
encoding, too.

However, if you just want to skip a few characters, I'd use Read if I
were you.

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

Nov 16 '05 #12

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

Similar topics

0
by: bullshark | last post by:
The code ran for 133 hours without failing. This would be the 267th iteration of the (same) object and the code below is used 5 times per iteration giving 1335 cycles. It was halfway through...
1
by: Richard Steele (Basemap) | last post by:
I am reading a text file using the StreamReader class, I am looking for prices of goods i.e. DAB Radio £230.00 As I read each line, I notice that the £ (pound) sign is missing can anyone shed...
1
by: ATS | last post by:
HOWTO Make StreamReader class not block. Please help, I'm trying to use the Std-Out from a process I launch, to read back data from the process, until either the data I expcet is passed, or a...
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...
7
by: sweetpotatop | last post by:
Hello, I have a txt file which has a few lines of special characters: This is A line from the text file: ...
4
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...
1
by: garyusenet | last post by:
>From MSDN I'm trying to learn more about streamreader. I'm working my way down the MSDN definition. The first entry is the Public Constructor StreamReader. I'm fairly happy I have a basic grasp on...
1
by: cathy25 | last post by:
Hi, I am using the following code to read a file. DirectoryInfo dir = new DirectoryInfo(@"C:\"); try { if (dir.Exists) { ...
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...
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
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...

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.