newLISP Fan Club

Forum => Anything else we might add? => Topic started by: tburton on July 21, 2006, 09:13:02 PM

Title: vexing problem or bug?
Post by: tburton on July 21, 2006, 09:13:02 PM
the following works fine:



(set 'out-buff "Now is the time to try men's souls")

(write-file "soulTest1.txt" out-buff)




but this doesn't:



(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)




What's going on here?  Many thanks.
Title:
Post by: cormullion on July 22, 2006, 12:22:35 AM
I think out-file-handle is in fact an integer:


(integer? out-file-handle)
;-> true


but write-file wants a string...
Title:
Post by: Lutz on July 22, 2006, 01:32:24 AM
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.
(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
Title:
Post by: tburton on July 22, 2006, 08:12:52 AM
Thanks; that clarifies.  



Tim