StreamReader bug?

G

Guest

I am trying to use the StreamReader to read through a simple text file. The
file, however, is not necessarily read in order. I can manipulate the
position of the current stream using the base stream's Seek method.
Subsequent checks of the Position property indicate that the seek was
successful. However, once the end of the file is reached it would appear
that any attempt to change the stream position is successful, but
StreamReader.Peek ALWAYS returns -1. Is this a bug in the framework
(doubtful), or am I doing something wrong (probable)?

The following code exhibits the behavior I am describing when compiled and
run against the latest version of the 1.0 framework:

--------------

using System;
using System.IO;

namespace PeekBugTest
{
/// <summary>
///
/// </summary>
class PeekBugTest
{
/// <summary>
///
/// </summary>
[STAThread]
static void Main(string[] args)
{
if (args.Length < 1)
{
Console.Error.WriteLine("Usage: PeekBugTest <text-file>");
return;
}

StreamReader s = new StreamReader(args[0]);

Console.WriteLine("File position is {0}.", s.BaseStream.Position);
Console.WriteLine("Outputting contents of " + args[0]);

while (s.Peek() != -1)
{
Console.WriteLine(s.ReadLine());
}

Console.WriteLine("File position is {0}.", s.BaseStream.Position);
Console.WriteLine("Seeking to the beginning.");

s.BaseStream.Seek(0, SeekOrigin.Begin);

Console.WriteLine("File position is {0}.", s.BaseStream.Position);
Console.WriteLine("Re-reading file. Output is:\r\n");

while (s.Peek() != -1)
{
Console.WriteLine(s.ReadLine());
}

s.Close();
}
}
}

---------------

You can cut and paste the following into a text file to use as input:

---------------
Line 1
Line 2
Line 3

---------------

The output that I receive running this from the command line is as follows:

C:\>PeekBugTest file.txt
File position is 0.
Outputting contents of file.txt
Line 1
Line 2
Line 3
File position is 24.
Seeking to the beginning.
File position is 0.
Re-reading file. Output is:

----------------------

No, I didn't forget to paste the second run. The file simply isn't there.
If tracing through the code in debug mode, the second peek returns -1.

Any help is greatly appreciated.

Andy Meadows
 
G

Gary Chang

Hi Andy,

Currently I am looking for somebody who could help you on it. We will reply
here with more information as soon as possible.
If you have any more concerns on it, please feel free to post here.


Thanks for your understanding!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
J

Jon Skeet [C# MVP]

Andy Meadows said:
I am trying to use the StreamReader to read through a simple text file. The
file, however, is not necessarily read in order. I can manipulate the
position of the current stream using the base stream's Seek method.
Subsequent checks of the Position property indicate that the seek was
successful. However, once the end of the file is reached it would appear
that any attempt to change the stream position is successful, but
StreamReader.Peek ALWAYS returns -1. Is this a bug in the framework
(doubtful), or am I doing something wrong (probable)?

You should use StreamReader.DiscardBufferedData() after seeking on the
base stream, otherwise it will remember that it had previously reached
the end of the stream.
 
A

Andy Meadows

That got it. Thanks.


Jon Skeet said:
You should use StreamReader.DiscardBufferedData() after seeking on the
base stream, otherwise it will remember that it had previously reached
the end of the stream.
 

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