for loop

G

Gigs_

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,108,105,103,104,116};
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.WriteLine(str);
Console.WriteLine(i);
}
}

why this for loop cant be like this?

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


thanks!
 
B

BobF

Gigs_ said:
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,108,105,103,104,116};
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.WriteLine(str);
Console.WriteLine(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
 
J

Jon Skeet [C# MVP]

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;
}
 
N

Nicholas Paldino [.NET/C# MVP]

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.Append((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]
- (e-mail address removed)



BobF said:
Gigs_ said:
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,108,105,103,104,116};
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.WriteLine(str);
Console.WriteLine(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
 

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