for loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gigs_

    #1

    for loop

    public class StreamsIOApp
    {
    [STAThread]
    public static void Main(string[] args)
    {
    // create, write, close, open, read, close
    byte[] buf1 = new Byte[]
    {76,101,116,32, 116,104,101,114 ,101,
    32,98,101,32,10 8,105,103,104,1 16};
    FileStream s = new FileStream(
    "Foo.txt", FileMode.Create );
    s.Write(buf1, 0, buf1.Length);
    s.Close();

    s = new FileStream(
    "Foo.txt", FileMode.Open);
    int i;
    string str = "";
    if (s.CanRead)
    {
    for (i = 0; (i = s.ReadByte()) != -1; i++)
    {
    str += (char)i;
    }
    }
    s.Close();
    Console.WriteLi ne(str);
    Console.WriteLi ne(i);
    }
    }

    why this for loop cant be like this?

    for (i = s.ReadByte(); i != -1; i++)
    {
    str += (char)i;
    }


    thanks!
  • BobF

    #2
    Re: for loop


    "Gigs_" <gigs@hi.t-com.hrwrote in message
    news:fjm30c$503 $1@ss408.t-com.hr...
    public class StreamsIOApp
    {
    [STAThread]
    public static void Main(string[] args)
    {
    // create, write, close, open, read, close
    byte[] buf1 = new Byte[]
    {76,101,116,32, 116,104,101,114 ,101,
    32,98,101,32,10 8,105,103,104,1 16};
    FileStream s = new FileStream(
    "Foo.txt", FileMode.Create );
    s.Write(buf1, 0, buf1.Length);
    s.Close();
    >
    s = new FileStream(
    "Foo.txt", FileMode.Open);
    int i;
    string str = "";
    if (s.CanRead)
    {
    for (i = 0; (i = s.ReadByte()) != -1; i++)
    {
    str += (char)i;
    }
    }
    s.Close();
    Console.WriteLi ne(str);
    Console.WriteLi ne(i);
    }
    }
    >
    why this for loop cant be like this?
    >
    for (i = s.ReadByte(); i != -1; i++)
    {
    str += (char)i;
    }
    >
    >
    thanks!
    i = s.ReadByte() will only happen once. Consider do ... while


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: for loop

      Gigs_ <gigs@hi.t-com.hrwrote:

      <snip>
      why this for loop cant be like this?
      >
      for (i = s.ReadByte(); i != -1; i++)
      {
      str += (char)i;
      }
      The initialization part of the "for" expression (i.e. the part before
      the first semi-colon) is executed once, at the start of the loop. The
      middle part is tested before each iteration of the loop, and the last
      part is executed at the end of the loop.

      Personally if I were writing that loop (which I wouldn't, due to its
      use of string concatenation and assumption of byte->char conversion)
      I'd do:

      int data;
      while ( (data = s.ReadByte()) != -1)
      {
      str += (char)data;
      }

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      World class .NET training in the UK: http://iterativetraining.co.uk

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: for loop

        Actually, you should be able to get away with it in a for loop:

        for (i = s.ReadByte(); i != -1; i = s.ReadByte())
        {
        str += (char) i;
        }

        Of course, for something like this, it is better to use a StringBuilder:

        StringBuiler stringBuilder = new StringBuilder() ;

        for (i = s.ReadByte(); i != -1; i = s.ReadByte())
        {
        stringBuilder.A ppend((char) i);
        }

        But ultimately, you don't have to do any of this, as the you should be
        able to create a StreamReader, pass an encoding, and then call ReadToEnd,
        which will produce the string for you without the loop.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m



        "BobF" <rNfOrSePeAzMe@ charter.netwrot e in message
        news:%23FV6ISDP IHA.4440@TK2MSF TNGP06.phx.gbl. ..
        >
        "Gigs_" <gigs@hi.t-com.hrwrote in message
        news:fjm30c$503 $1@ss408.t-com.hr...
        > public class StreamsIOApp
        > {
        > [STAThread]
        > public static void Main(string[] args)
        > {
        > // create, write, close, open, read, close
        > byte[] buf1 = new Byte[]
        > {76,101,116,32, 116,104,101,114 ,101,
        > 32,98,101,32,10 8,105,103,104,1 16};
        > FileStream s = new FileStream(
        > "Foo.txt", FileMode.Create );
        > s.Write(buf1, 0, buf1.Length);
        > s.Close();
        >>
        > s = new FileStream(
        > "Foo.txt", FileMode.Open);
        > int i;
        > string str = "";
        > if (s.CanRead)
        > {
        > for (i = 0; (i = s.ReadByte()) != -1; i++)
        > {
        > str += (char)i;
        > }
        > }
        > s.Close();
        > Console.WriteLi ne(str);
        > Console.WriteLi ne(i);
        >}
        > }
        >>
        >why this for loop cant be like this?
        >>
        >for (i = s.ReadByte(); i != -1; i++)
        >{
        > str += (char)i;
        > }
        >>
        >>
        >thanks!
        >
        i = s.ReadByte() will only happen once. Consider do ... while
        >

        Comment

        Working...