Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

StreamReader.BaseStream.Seek fails

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
viepia@nospam.com - 07 Mar 2008 06:21 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.