You need to specify :EXTERNAL-FORMAT
argument, mentioning the line terminator mode:
(with-open-file (out-file "test.dat" :direction :output :external-format :unix)
...)
The external format defaults to :dos
on windows because that is the standard on Microsoft systems.
Note that you do not want binary mode if you are actually writing text. In Common Lisp (as opposed to C and Emacs Lisp), there is a very clear separation between binary i/o (reading and writing byte
s) and text i/o (reaching and writing character
s), just like a number is not a character and vice versa, even though characters have an integer code.