That happens because you are writing to an already existing file that has a larger size than the data you are writing. From the
OpenWrite[
^] description:
Quote:
If you overwrite a longer string (such as “This is a test of the OpenWrite method”) with a shorter string (such as “Second run”), the file will contain a mix of the strings (“Second runtest of the OpenWrite method”).
If you create a new file or delete it before, the size will be 4.
[UPDATE]
If you want existing files to be truncated, use
File.Open()
with the appropiate mode or just
File.Create()[
^]:
BinaryWriter sr = new BinaryWriter(File.Create("bin.txt"));
File.OpenWrite()
uses
FileMode.OpenOrCreate
while
File.Create()
uses
FileMode.Create
. See
FileMode[
^] for a description of the modes.