Arc Forumnew | comments | leaders | submitlogin
How do I write in a file without any "double quotes" around the text?
4 points by lark 4415 days ago | 8 comments
The following generates double quotes around what goes into the file. I don't want that.

  arc> (writefile "this is some text" "tmpfile")
  "this is some text"
  $ cat tmpfile 
  "this is some text"


1 point by Pauan 4414 days ago | link

As akkartik said, the problem is that "write" wraps strings in double-quotes. Oddly enough, Arc doesn't provide dispfile, which would do what you want. There's two ways to implement dispfile: the simple way...

  (def dispfile (val file)
    (w/outfile f file
      (disp val f))
    val)
...or by copy+pasting writefile and changing it to use disp:

  (def dispfile (val file)
    (let tmpfile (+ file ".tmp")
      (w/outfile o tmpfile (disp val o))
      (mvfile tmpfile file))
    val)
I personally would go for the simple way. I dislike the way Arc handles temporary files, so if I wanted to use temp files + move, I'd end up changing the entire temporary file implementation...

-----

3 points by akkartik 4414 days ago | link

For comparison, I think this is the shortest way to do this without introducing a new name:

  (tofile "tmpfile" (disp "this is some text"))
---

How would you better implement writefile? I think the goal is to avoid corrupting a file that gets repeatedly updated. If the disk fills up, for example, you might end up losing the previous version and unable to fully write the new version.

-----

1 point by zck 4414 days ago | link

What don't you like about the way Arc handles temporary files? Is that you can't have another file named that? Arc could use `(file-gensym)` instead, if that was a thing.

-----

2 points by Pauan 4407 days ago | link

To put it simply, I think Arc should use Racket's support for temporary files:

http://docs.racket-lang.org/reference/Filesystem.html?q=temp...

-----

1 point by akkartik 4407 days ago | link

Ok, done: http://github.com/nex3/arc/commit/df9fd21eeb

Thanks for the tip! I had no idea there could be a better way. Now I find that my use of $RANDOM in shell scripts should be replaced with calls to mktemp as well.

-----

1 point by akkartik 4413 days ago | link

Ah, good point. Here's a quick-and-dirty fix:

http://github.com/nex3/arc/commit/c70a51a4d5

Now tofile does the 'write to tmp file and move' dance. writefile is implemented in terms of tofile. Finally, the tmp file is more randomly chosen, so it should be thread-safe.

-----

2 points by akkartik 4415 days ago | link

The basic output primitives in arc are write vs disp.

  arc> (write "a")
  "a"
  arc> (disp "a")
  a
These mirror racket's write vs display.

write corresponds to read; its output is meant to be read back in by arc. Hence the quotes.

writefile is just a wrapper around write.

-----

1 point by lark 4415 days ago | link

Well, I'm incompetent, but this works:

  ;; write in a fine file                                                                                                 
  (def wiff (file contents)
       (w/outfile p file (each b contents (writec b p))))

-----