Warning: typos may follow...
(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)
(let ((len (file-length file)))
(setf (data (make-array len :element-type 'character
:fill-pointer 0 :adjustable t))
(read-sequence data file :start 0 :end len)))
(values data))) ; ?
Hi, I'm new and I'm trying to load a file into a string and
return it, but this just returns an empty string. I guess
the array is going out of scope? How should I write this?
Thanks for any help!
Matt
It works if you use
(setf data (make-array len :element-type 'character))
instead of
(setf data (make-array len :element-type 'character
:fill-pointer 0 :adjustable t))
[you wrote "setf (data", but I imagine the extra parenthesis was not
present in the code you tested]
Googling for "slurp file common lisp" returns a couple of useful
pointers.
············@gmail.com wrote:
> It works if you use
> (setf data (make-array len :element-type 'character))
> instead of
> (setf data (make-array len :element-type 'character
> :fill-pointer 0 :adjustable t))
> [you wrote "setf (data", but I imagine the extra parenthesis was not
> present in the code you tested]
Yeah, that was one of those typos I mentioned. My internet
computer is trapped in a lexical fence :-)
> Googling for "slurp file common lisp" returns a couple of useful
> pointers.
Thanks, I should have thought of that, but I'm braindead sometimes.
Thanks!
Matt
Matt wrote:
>
> Hi, I'm new and I'm trying to load a file into a string and
> return it, but this just returns an empty string. I guess
> the array is going out of scope? How should I write this?
Maybe it will be interesting for you:
http://www.emmett.ca/~sabetts/slurp.html
z0d wrote:
> Matt wrote:
>>
>> Hi, I'm new and I'm trying to load a file into a string and
>> return it, but this just returns an empty string. I guess
>> the array is going out of scope? How should I write this?
>
> Maybe it will be interesting for you:
> http://www.emmett.ca/~sabetts/slurp.html
Yeah, I saw that one, thanks. Looking at the last version on
there, I guess I don't need the call to (values) at the end. I
guess I knew that, but damn! It's hard to remember all the
rules sometimes! :-)
Matt
"z0d" <·············@gmail.com> writes:
> Maybe it will be interesting for you:
> http://www.emmett.ca/~sabetts/slurp.html
The last version doesn't work correctly in environments where the
conversion from bytes to characters changes the length, like newline
translation or UTF-8 decoding.
--
__("< Marcin Kowalczyk
\__/ ······@knm.org.pl
^^ http://qrnik.knm.org.pl/~qrczak/
Matt wrote:
> Warning: typos may follow...
>
> (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)
> (let ((len (file-length file)))
> (setf (data (make-array len :element-type 'character
> :fill-pointer 0 :adjustable t))
> (read-sequence data file :start 0 :end len)))
> (values data))) ; ?
>
>
> Hi, I'm new and I'm trying to load a file into a string and
> return it, but this just returns an empty string. I guess
> the array is going out of scope? How should I write this?
>
> Thanks for any help!
> Matt
Change :fill-pointer 0 to :fill-pointer t in the code. With
fill-pointer 0 the length is essentially 0 (empty string).
Wade
Wade Humeniuk wrote:
> Matt wrote:
>
>> Hi, I'm new and I'm trying to load a file into a string and
>> return it, but this just returns an empty string. I guess
>> the array is going out of scope? How should I write this?
>>
>> Thanks for any help!
>> Matt
>
>
> Change :fill-pointer 0 to :fill-pointer t in the code. With
> fill-pointer 0 the length is essentially 0 (empty string).
>
> Wade
Thanks, that worked! I thought it said that read-sequence
acted like vector-push-extend in that it increments the fill
pointer as it reads, but I guess I was thinking of something
else.
Thanks!
Matt
Matt wrote:
> Wade Humeniuk wrote:
>> Matt wrote:
>>
>>> Hi, I'm new and I'm trying to load a file into a string and
>>> return it, but this just returns an empty string. I guess
>>> the array is going out of scope? How should I write this?
>>>
>>> Thanks for any help!
>>> Matt
>>
>>
>> Change :fill-pointer 0 to :fill-pointer t in the code. With
>> fill-pointer 0 the length is essentially 0 (empty string).
>>
>> Wade
>
> Thanks, that worked! I thought it said that read-sequence
> acted like vector-push-extend in that it increments the fill
> pointer as it reads, but I guess I was thinking of something
> else.
>
You are probably thinking of FORMAT.
Wade
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)))