vexing problem or bug?

For the Compleat Fan
Locked
tburton
Posts: 10
Joined: Fri Jun 30, 2006 3:02 am
Location: Oregon, USA

vexing problem or bug?

Post by tburton »

the following works fine:

[code](set 'out-buff "Now is the time to try men's souls")
(write-file "soulTest1.txt" out-buff) [/code]

but this doesn't:

[code](set 'out-buff "Now is the time to try men's souls")
(set 'out-file-handle (open "soulTest2.txt" "write"))
(write-file out-file-handle out-buff) [/code]

What's going on here? Many thanks.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

I think out-file-handle is in fact an integer:

Code: Select all

(integer? out-file-handle)
;-> true
but write-file wants a string...

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Yes, (write-file <str-file-name> <str-content>) is for writing an entire file in one shot.
Use (write-buffer <int-handle> <str-content>) if you want to write to a file handle, which also must be closed after writing.

Code: Select all

(set 'out-buff "Now is the time to try men's souls")
(set 'out-file-handle (open "soulTest2.txt" "write"))
(write-buffer out-file-handle out-buff)
(close out-file-handle)
Most of the time write-file does the job much easier in one statement. Use write-buffer only for bigger files, or when writing to a string buffer for fast destructive string appending (see manual).

Lutz

tburton
Posts: 10
Joined: Fri Jun 30, 2006 3:02 am
Location: Oregon, USA

Post by tburton »

Thanks; that clarifies.

Tim

Locked