The following code produces an infinite loop and I was hoping someone could explain why. It has something to do with trying to use a byte value in a FOR loop. Since a byte is simply an 8-bit unsigned integer, I don't see why I shouldn't be able to use it natively in a FOR loop instead of typecasting a 32-bit integer when I write to the file.
using (FileStream fS = new FileStream(FileToWrite, FileMode.Create))
using (BinaryWriter bW = new BinaryWriter(fS))
{
for (int i = 0; i < 10; i++)
{
for (byte j = 0; j <= 255; j++) // This for loop increments and writes correctly, but loops infinitely.
{
bW.Write(j);
}
}
bW.Close();
}
Question
boogerjones
The following code produces an infinite loop and I was hoping someone could explain why. It has something to do with trying to use a byte value in a FOR loop. Since a byte is simply an 8-bit unsigned integer, I don't see why I shouldn't be able to use it natively in a FOR loop instead of typecasting a 32-bit integer when I write to the file.
using (FileStream fS = new FileStream(FileToWrite, FileMode.Create)) using (BinaryWriter bW = new BinaryWriter(fS)) { for (int i = 0; i < 10; i++) { for (byte j = 0; j <= 255; j++) // This for loop increments and writes correctly, but loops infinitely. { bW.Write(j); } } bW.Close(); }Link to comment
Share on other sites
8 answers to this question
Recommended Posts