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