s c h e m a t i c s : c o o k b o o k

/ Cookbook.FileWrite

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Writing to a File

Problem

You want to write text or data to a file.

Solution

If you just have a string s to write to a file with name fname, this will work: 500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

Or, for the truly lazy, you can redirect standard out with with-output-to-file and just use display with no port argument:

500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

If you have a list of strings, you can join the list into a string, or you can write something like this (assuming string-list is your list of strings): 500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

Discussion

PLT's implementation of call-with-output-file takes optional arguments on the end. 'replace is a very important one, without this flag, call-with-output-file will fail if the file already exists: %begin scheme% > (call-with-output-file fname
(lambda (p) (display "Hello, World!" p))) > (call-with-output-file fname
(lambda (p) (display "Hello, World!" p))) call-with-output-file: file "c:/temp/test.txt" exists > (call-with-output-file fname
(lambda (p) (display "Hello, again, World!" p)) 'replace) %end

'truncate works slightly different from 'replace, 'replace is documented to remove the old file before writing a new one, while 'truncate is documented to set the file length to 0 before writing. You can also use 'append to append to the end of a file, or 'update, which raises an exception if the file does not exist.


Comments about this recipe

Contributors

-- GordonWeakliem - 13 Aug 2004

CookbookForm
TopicType: Recipe
ParentTopic: FileRecipes
TopicOrder: 999

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest