Hi,
using System;
using System.IO;
namespace testBaseStream
{
class Program
{
static void Main(string[] args)
{
string fn = "temp.txt";
StreamWriter sw = new StreamWriter(fn);
sw.WriteLine("123456");
sw.Close();
StreamReader sr = new StreamReader(fn);
sr.BaseStream.Seek(3,SeekOrigin.Begin);
char[] c = new char[1];
sr.Read(c, 0, 1);
Console.WriteLine(String.Format("Seek 3: {0} Should be: 4",c[0]));
sr.BaseStream.Seek(2, SeekOrigin.Begin);
sr.Read(c, 0, 1);
Console.WriteLine(String.Format("Seek 2: {0} Should be: 3", c[0]));
sr.BaseStream.Seek(1, SeekOrigin.Begin);
sr.Read(c, 0, 1);
Console.WriteLine(String.Format("Seek 1: {0} Should be: 2", c[0]));
Console.ReadLine();
}
}
}
results:
Seek 3: 4 Should be: 4
Seek 2: 5 Should be: 3
Seek 1: 6 Should be: 2
StreamReader.BaseStream.Seek worked the first time it was called, but not the 2nd or 3rd.
BinaryReader.BaseStream.Seek and BinaryWriter.BaseStream.Seek both work.
Viepia
Jon Skeet [C# MVP] - 07 Mar 2008 07:41 GMT
<snip>
> StreamReader.BaseStream.Seek worked the first time it was called, but
> not the 2nd or 3rd.
>
> BinaryReader.BaseStream.Seek and BinaryWriter.BaseStream.Seek both work.
From the docs for StreamReader.BaseStream:
<quote>
StreamReader might buffer input such that the position of the
underlying stream will not match the StreamReader position.
</quote>
Unfortunately the docs don't explain how to get round this: call
DiscardBufferedData() if you make changes to the stream.
Thanks very much for including a short but complete program - it made
it trivial to confirm that this was the problem. (I guess what it would
probably be just from reading the subject, btw :)
One thing you may wish to consider is using the Stream.Position
property instead of Seek - I find it more readable.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Michael Nemtsev [MVP] - 07 Mar 2008 07:50 GMT
Hello viepia@nospam.com,
StreamReader is a bit tricky it that way, because It silently cache your
data and
whenever u wanna change Seek u need to call DiscardBufferedData before
---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour
"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
> Hi,
> using System;
[quoted text clipped - 38 lines]
>
> Viepia
viepia@nospam.com - 07 Mar 2008 12:35 GMT
DiscardBufferedData works, thanks very much!
Viepia
using System;
using System.IO;
namespace testBaseStream
{
class Program
{
static void Main(string[] args)
{
string fn = "temp.txt";
StreamWriter sw = new StreamWriter(fn);
sw.WriteLine("123456");
sw.Close();
StreamReader sr = new StreamReader(fn);
sr.BaseStream.Seek(3,SeekOrigin.Begin);
char[] c = new char[1];
sr.Read(c, 0, 1);
Console.WriteLine(String.Format("Seek 3: {0} Should be: 4",c[0]));
sr.DiscardBufferedData();
sr.BaseStream.Seek(2, SeekOrigin.Begin);
sr.Read(c, 0, 1);
Console.WriteLine(String.Format("Seek 2: {0} Should be: 3", c[0]));
sr.DiscardBufferedData();
sr.BaseStream.Seek(1, SeekOrigin.Begin);
sr.Read(c, 0, 1);
Console.WriteLine(String.Format("Seek 1: {0} Should be: 2", c[0]));
Console.ReadLine();
}
}
}
results:
Seek 3: 4 Should be: 4
Seek 2: 3 Should be: 3
Seek 1: 2 Should be: 2
>DiscardBufferedData