[cl-cookbook-contrib] [ cl-cookbook-Patches-1522710 ] File Slurping
Brought to you by:
jthing
From: SourceForge.net <no...@so...> - 2006-07-17 13:09:49
|
Patches item #1522710, was opened at 2006-07-14 11:12 Message generated for change (Comment added) made by nobody You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=447474&aid=1522710&group_id=46815 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: Widget (example) Group: None Status: Open Resolution: None Priority: 5 Submitted By: Nobody/Anonymous (nobody) Assigned to: Nobody/Anonymous (nobody) Summary: File Slurping Initial Comment: I looked through the file section of the cookbook and found no simple way to get the string contents from a text file. with the-file being a pathname, this snippet returns the string contents of a file: (with-open-file (f the-file) (let ((seq (make-string (file-length f)))) (read-sequence seq f) (coerce seq 'string)) ) I added the coerce at the bottom, but got the main part from http://www.emmett.ca/~sabetts/slurp.html According to this article, this is also the fastest. ---------------------------------------------------------------------- Comment By: Nobody/Anonymous (nobody) Date: 2006-07-17 06:09 Message: Logged In: NO The above leaves extra characters at end of string. From what I read it looks like it has something to do with linefeeds in ms-dos/windows. This code works with the file I'm testing (got it from http://www.codecomments.com/archive274-2006-1-786774.html): (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)))) ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=447474&aid=1522710&group_id=46815 |