var stream1 = new BinaryReader(File.OpenRead("someFile1.bin"));
var stream2 = new BinaryReader(File.OpenRead("someFile2.bin"));
var output = new BinaryWriter(File.OpenWrite("output.bin"));
if (stream1.PeekChar() != -1 && stream2.PeekChar() != -1)
{
var val1 = stream1.ReadInt32();
var val2 = stream2.ReadInt32();
while (true)
{
if (val1 == val2)
{
output.Write(val1);
if (stream1.PeekChar() == -1 || stream2.PeekChar() == -1) break;
val1 = stream1.ReadInt32();
val2 = stream2.ReadInt32();
}
else if (val1 < val2)
{
if (stream1.PeekChar() == -1) break;
val1 = stream1.ReadInt32();
}
else
{
if (stream2.PeekChar() == -1) break;
val2 = stream2.ReadInt32();
}
}
}
stream1.Close();
stream2.Close();
output.Close();