StreamReader.BaseStream.Seek fails

V

viepia

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
 
J

Jon Skeet [C# MVP]

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.
 
M

Michael Nemtsev [MVP]

Hello (e-mail address removed),

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
 
V

viepia

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top