Reputation: 19
BinaryWriter bw;
while(bw.BaseStream.Position < 192137)
bw.Write((byte) 0);
At the end, bw.BaseStream.Position
equals 192152 (not 192137!). And the file size is 192 104 bytes. How is this possible?
Upvotes: 1
Views: 1373
Reputation: 217341
BinaryWriter buffers the data before writing it to the underlying Stream.
If you want to write 192137 bytes, write 192137 bytes to the BinaryWriter instead of waiting until 192137 bytes have been written to the underlying Stream:
for (int i = 0; i < 192137; i++)
{
bw.Write((byte) 0);
}
Rule of thumb: Use Stream or BinaryWriter, but don't use them both. (I.e., if you use a BinaryWriter, don't access the BaseStream.)
Upvotes: 1