Re: read the whole file into a string?

Martin Gasbichler <[email protected]>
Newsgroups gmane.lisp.scheme.scsh
Message-ID <[email protected]>
William Xuuu <[email protected]> writes:

> Hi,
>
> A simple question, how to read a file into one single string? I looked into
> the manuals of scsh, only found this:
>
> (read-string nbytes [fd/port])     --->     string or #f (procedure)
> (read-string! str [fd/port start end]) --->     nread or #f (procedure)
>
> I took a try:
>
> (let* ((port (open-file "/tmp/foo" open/read))
>        (str (read-string 100 port)))
>   (display str))
>
> Can someone give me a hint? 

You're almost there. All you need to add is passing the size of the
file to READ-STRING:

(let* ((port (open-file "/etc/passwd" open/read))
       (str (read-string (file-size "/etc/passwd") port)))
  (display str))

-- 
Martin
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.