Search code examples
schemechicken-scheme

how do i write a string to a file without quote marks in scheme


I'm trying to write a string to a file, but every time i do it has quotes around it.

I've tried

(call-with-output-file file-path
  (lambda(output-port)(write "some text" output-port)))

and

(let ((p (open-output-file file-path)))
      (write "some text" p)
      (close-output-port p))

but in both cases i expected "some text" but got "\"some text\""

I'm currently working in chicken-scheme but I don't think that matters.


Solution

  • write is for serializing S-expressions to a file. It is the opposite of read, which will read a serialized S-expression back into lists, symbols, strings and so on. That means write will output everything like it would occur in source code.

    If you just want to output a string to a port, use display:

    (call-with-output-file file-path
      (lambda(output-port)
        (display "some text" output-port)))
    

    Or in CHICKEN, you can use printf or fprintf:

    (call-with-output-file file-path
      (lambda(output-port)
        (fprintf output-port 
                 "Printing as s-expression: ~S, as plain string: ~A"
                 "some text"
                 "some other test")))
    

    This will print the following to the file:

    Printing as s-expression: "some text", as plain string: some other text