Вопрос

I'm writing this program that's supposed to read from a file, do some stuff with the content and write to an output file preserving the original line endings. If the file has CRLF endings, the output file should also have that. My problem is in writing the line ending especially with the CLISP implementation(it works with gcl). When I try to write a linefeed character(LF), the file ends up having CRLF endings. I'm guessing this is something to do with CLISP's implementation.
I need a way to write the file in binary mode like in other langauages. The standard I/O functions in the specification only take an optional stream name and the content to be written.

You can reproduce that behaviour with something like this:

(with-open-file (out-file "test.dat" :direction :output)
  (setf ending #\linefeed)
  (princ "First Line" out-file)
  (write-char ending out-file)
  (princ "Second Line" out-file)
  (write-char ending out-file)
  (princ "Second Line" out-file))

I need a solution that works in windows.

Это было полезно?

Решение

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 bytes) and text i/o (reaching and writing characters), just like a number is not a character and vice versa, even though characters have an integer code.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top