Re: slurp-file



I wound up writing it like this because I guess MS-DOS newlines
screw up the file size and makes the array length longer than
the data that was really read in.  I'm not really sure, but if I
turn around and write the data to another file, it leaves a bunch
of garbage at the end unless I explicitly set the fill pointer
with the return value of read-sequence.

(defun slurp-file (name)
  "Slurps up file <name> and returns the data as a string."
  (let ((data nil))
    (with-open-file (file name :direction :input)
      (setf data (make-array (file-length file) :element-type 'character
                              :fill-pointer t :adjustable t))
      (setf (fill-pointer data) (read-sequence data file))
    (values data))))

(defun spew-data (data name)
  "Spews string <data> to file <name>."
  (with-open-file (file name :direction :output)
    (write-sequence data file)))

(defun copy-file (src dest)
  (let ((data (slurp-file src)))
    (spew-data data dest)))
.



Relevant Pages

  • Re: modifying the strings pointed to by argv
    ... If you screw up and write too much into an argv array, ... likely to trash something important than if you wrote too much into ...
    (comp.lang.c)
  • Re: Not fully comprehending arrays and dynamic memory.
    ... Dig it! ... The problem is that whenever I try to use pointer arithmetic I screw up. ... I just want to use conventional array type statements to get at the memory. ... You can allocate one like this: ...
    (comp.lang.c)
  • Re: modifying the strings pointed to by argv
    ... If you screw up and write too much into an argv array, ... more likely to trash something important than if you wrote too much ... Isn't there a school of thought that says that if you screw up it's ...
    (comp.lang.c)
  • Re: Unlimited Array in Visual Basic 8
    ... I'm a very fresh beginner to VB.Net, so hopefully I won't screw this up, but ... Declare your counter variable ... Declare your array ...
    (microsoft.public.dotnet.languages.vb)