Hi Guys
NP118 different result
http://www.aspfree.com/c/a/C-Sharp/C-Sharp-FileStream-Explained/3/
Following program is given in the above website. When “Start Without Debugging” is used program is producing following result:
Investigating the file capabilities
Can Read? True
Can Write? False
Can Seek? True
Before we start reading bytes the current position: 0
After we have read the bytes the current position: 0
When “Step Into” (F11) key is used same program is producing different result.
Please explain the reason for the variation.
Please tell me how to get the output expected by author.
Thank you
using System;
using System.IO;
namespace MyStreams
{
class Class1
{
public static void Main()
{
try
{
using(FileStream fStream = File.OpenRead("aFile.txt"))
{
Console.WriteLine("Investigating the file capabilities");
Console.WriteLine("Can Read? {0}", fStream.CanRead);
Console.WriteLine("Can Write? {0}", fStream.CanWrite);
Console.WriteLine("Can Seek? {0}", fStream.CanSeek);
Console.WriteLine("Before we start reading bytes the current position: {0}", fStream.Position);
if(fStream.CanRead)
{
// returns the bytes of the file
byte[] bytes = new byte[fStream.Length];
fStream.Read(bytes, 0, bytes.Length);
foreach(byte b in bytes)
{
Console.Write(" {0} ", b);
}
/* This code can be used in place of the above code
* int temp = 0;
while((temp = fStream.ReadByte()) != -1)
{
Console.WriteLine(temp);
}*/
/* This code can be used in place of the above while statement
* while(fStream.Position < fStream.Length)
{
Console.Write(" {0} ", fStream.ReadByte());
}
* */
}
Console.WriteLine("nAfter we have read the bytes the current position: {0}",fStream.Position);
Console.ReadLine();
}
}
catch(IOException ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
AlanPosted Aug 10, 2008, 10:49 AM
The output was just the same when I tried it just now.
Did you create the file aFile.txt (containing ABCD) and copy it to the \bin\debug folder of the current application?
Incidentally, there's a mistake in this line:
Console.WriteLine("nAfter we have read the bytes the current position: {0}",fStream.Position);
which should be:
Console.WriteLine("\nAfter we have read the bytes the current position: {0}",fStream.Position);
Posted Aug 11, 2008, 10:56 AM
Thank you for your explanation.
Microsoft Visual C# 2008 Express Edition is creating a “Release” folder inside the “bin” folder (C:\x\Use the Seek()\Use the Seek()\bin\Release\aFile.txt'.), program is to execute perfectly aFile.txt' must be in the Release folder.
But in Microsoft VS.NET 2003 there is no Release folder. Instead aFile.txt must be in the Debug folder which is in bin folder.
AlanPosted Aug 10, 2008, 1:34 PM
I used VS 2005 in fact rather than 2003 but I suspect that the problem when VC# 2008 Express is used is that, initially, the application is stored in a temporary projects folder which is not the same as the folder which it will eventually be saved to. You can check where it's stored by executing this line:
Console.WriteLine(Environment.CurrentDirectory);
I'd try replacing "afile.txt" with the full path to where you have it stored and see if that solves the problem.
Posted Aug 10, 2008, 12:31 PM
Thank you for your help.
Instead of Microsoft VS.NET 2003 I used Microsoft Visual C# 2008 Express Edition. This was the one of the reason for different output. When I used Microsoft VS.NET 2003, output is okay. Please explain why Express Edition gave different output.