From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Mark H Weaver Newsgroups: gmane.lisp.guile.user Subject: Re: I/O, modules Date: Tue, 13 Nov 2012 09:31:05 -0500 Message-ID: <87fw4defrq.fsf@tines.lan> References: <42782.199.48.147.41.1352786231.squirrel@lavabit.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1352817108 2078 80.91.229.3 (13 Nov 2012 14:31:48 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 13 Nov 2012 14:31:48 +0000 (UTC) Cc: guile-user@gnu.org To: thorsopia@lavabit.com Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Tue Nov 13 15:31:59 2012 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1TYHWv-0002ER-7d for guile-user@m.gmane.org; Tue, 13 Nov 2012 15:31:57 +0100 Original-Received: from localhost ([::1]:33362 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TYHWj-00039l-A1 for guile-user@m.gmane.org; Tue, 13 Nov 2012 09:31:45 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:59487) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TYHWY-0002ly-Ns for guile-user@gnu.org; Tue, 13 Nov 2012 09:31:37 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TYHWV-0007Tp-MH for guile-user@gnu.org; Tue, 13 Nov 2012 09:31:34 -0500 Original-Received: from world.peace.net ([96.39.62.75]:37387) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TYHWV-0007R2-Hj for guile-user@gnu.org; Tue, 13 Nov 2012 09:31:31 -0500 Original-Received: from 209-6-91-212.c3-0.smr-ubr1.sbo-smr.ma.cable.rcn.com ([209.6.91.212] helo=tines.lan) by world.peace.net with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72) (envelope-from ) id 1TYHWD-00089x-JE; Tue, 13 Nov 2012 09:31:13 -0500 In-Reply-To: <42782.199.48.147.41.1352786231.squirrel@lavabit.com> (thorsopia@lavabit.com's message of "Tue, 13 Nov 2012 00:57:11 -0500 (EST)") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 96.39.62.75 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:9670 Archived-At: thorsopia@lavabit.com writes: > Could you show me some trivial programs related to I/O (e.g. read from > file, convert to uppercase, write to another file)? There are many ways to do this, but here's a simple upcase program that reads the entire file into memory and does not depend on any external modules: --8<---------------cut here---------------start------------->8--- #!/usr/bin/guile \ -e main -s !# (use-modules (ice-9 match) (ice-9 rdelim)) (define (usage) (display "Usage: upcase \n") (exit 1)) (define (upcase-file in-filename out-filename) (call-with-input-file in-filename (lambda (in-port) (call-with-output-file out-filename (lambda (out-port) (display (string-upcase (read-delimited "" in-port)) out-port)))))) (define (main args) (setlocale LC_ALL "") (match args ((program in-filename out-filename) (upcase-file in-filename out-filename)) (_ (usage)))) --8<---------------cut here---------------end--------------->8--- > This page [0] doesn't list a function that can be used to read the whole > file at once. As shown in the upcase program above, (read-delimited "" port) will read the whole file as a string. You need to (use-modules (ice-9 rdelim)) to import this procedure. Most of these procedures allow you to omit the 'port' argument, in which case they will use (current-input-port) or (current-output-port). Those can be temporarily set within a block using 'with-input-from-file' and 'with-output-to-file'. For example, 'upcase-file' above could be written as follows: (define (upcase-file in-filename out-filename) (with-input-from-file in-filename (lambda () (with-output-to-file out-filename (lambda () (display (string-upcase (read-delimited "")))))))) To read one line from a file, use (read-line port), which is also from (ice-9 rdelim). For example, we can use this to modify the upcase program to read and write one line at a time, so that it will use less memory for large files (as long as the lines aren't too long): (define (upcase-file in-filename out-filename) (call-with-input-file in-filename (lambda (in-port) (call-with-output-file out-filename (lambda (out-port) (define (loop) (let ((line (read-line in-port 'concat))) (unless (eof-object? line) (display (string-upcase line) out-port) (loop)))) (loop)))))) The 'concat argument to 'read-line' means that it should include the end-of-line character(s), if any, in the returned string. By default they are stripped. Happy hacking! Mark