[plt-scheme] reading a whole file

From: Robby Findler (robby at cs.uchicago.edu)
Date: Tue Nov 4 09:46:11 EST 2008

With a reasonable filesystem (I'm not sure if Windows counts as sane
in this regard, but mac os x and linux's both do I believe), this will
work and not be subject to race conditions (all of the heavy lifting
is in copy-port):

#lang scheme
(require scheme/port)
(define (file->string f)
  (let ([sp (open-output-string)])
    (call-with-input-file f
      (λ (fp) (copy-port fp sp)))
    (get-output-string sp)))

but I do not recommend reading files into strings. Instead, it is much
better to just operate on the port if you possibly can.

Probably I or others can help if you're not sure how to write whatever
function you wanted directly on the port instead of on the string.

Robby

On Tue, Nov 4, 2008 at 9:42 AM, Shriram Krishnamurthi <sk at cs.brown.edu> wrote:
> The Cookbook version has a slight non-atomicity problem (the file size
> may change between the FILE-SIZE and the READ-STRING).  I don't know
> quite how that works out in the version Veer posted (presumably the
> same problem, because FILE-SIZE is being called on the file*name*, not
> on the port descriptor).  I guess file operation atomicity is
> OS-specific anyway.
>
> Shriram
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
>

Posted on the users mailing list.