Your script looks like it would work fine if it were fed pure Mac OS 9 or earlier text. Probably what's happening is you are dealing with text from Mac OS X, which has unix-style newlines, with \015 (\f) as the newline (not \r, which is \012, and was the traditional Mac newline before OS X). This will result in a square when reading on a Windows machine because it does not recognize \015 as a newline at all.
The actual newlines in your output come not from the original file but from your printing "\n" in the while loop, since \n is the "logical newline" for your system, in your case Windows, and thus generates the correct \012\015 sequence.
PS If I am correct about your using Mac OS X files then simply changing $/ to "\015" (untested and not sure if I have the syntax right) should solve your problem.