scsh-users
[Top] [All Lists]

Re: read the whole file into a string?

To: William Xuuu <william_xuuu@163.com>
Subject: Re: read the whole file into a string?
From: Martin Gasbichler <gasbichl@informatik.uni-tuebingen.de>
Date: Mon, 07 Feb 2005 19:29:15 +0100
Cc: scsh-users@scsh.net
List-id: <scsh-users.list-id.scsh.net>
William Xuuu <william_xuuu@163.com> 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

<Prev in Thread] Current Thread [Next in Thread>