[plt-scheme] reading a whole file

From: Robby Findler (robby at cs.uchicago.edu)
Date: Tue Nov 4 10:06:57 EST 2008

On Tue, Nov 4, 2008 at 10:04 AM, Stephen De Gabrielle
<s.degabrielle at cs.ucl.ac.uk> wrote:
> Wow - thanks for all the good advice,
>
> I'm guessing that (port->string [input-port]) in (planet "port.ss"
> ("schematics" "port.plt" 1 2))  is similar to the method below.

If you look at the code, you see it is identical. :)

> I asked the question - so I'll copy the answer into the cookbook.
>
> I understand the point about operating on files, but I feel this is
> the special case of large numbers of small files, read only, and at
> least 2gb RAM to work with. I may be wrong...

As long as you don't give the program out to others, you're probably fine.

But the ports library in plt scheme is powerful and many things that
seem easier on strings just aren't.

Robby

> Cheers,
>
> Stephen
>
> On Tue, Nov 4, 2008 at 2:46 PM, Robby Findler <robby at cs.uchicago.edu> wrote:
>> 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
>>>
>>>
>>
>> _________________________________________________
>>  For list-related administrative tasks:
>>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>
>>
>

Posted on the users mailing list.