4
Answers

NP118 different result

Maha

Maha

16y
2k
1

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

      }

    }

  }

}

Answers (4)