Abbie Normal is a user on mastodon.social. You can follow them or interact with them if you have an account anywhere in the fediverse. If you don't, you can sign up here.
Abbie Normal @catonano

I need to write a little file from code

I d love an example, I can t bother to read about ports 😑

guile manal can be especially daunting to browse 😑

· Web · 0 · 0

ok, it s

(open-output-file "output.scm")

@catonano Yep, though I usually use call-with-output-file or with-output-to-file which close the file automatically when you're finished.

@vbuaraujo

does call-with-output-port exist ?

@vbuaraujo

anyway I managed to have a file created

the problem now is that I expected something written in it

but it's empty🙄

@catonano If you use open-output-file, you have to remember to call close-port on it when you're finished. If you don't the contents may remain buffered and not get written to the file. Or you can call force-output on the port to flush the buffers.

@vbuaraujo

a that is probably what happened to me !

@vbuaraujo

I mean, there is:

Guile modules -> POSIX -> Ports and file descriptors

then ther is

API rreferrence -> Input and Output -> Port types -> File ports

Where open-output-file and open-input-file are found

And this "call-with-output-file" is not to be fond in any of these places, so it must be somewhere else

Whe ALL is needed is this:

(call-with-output-file "outpt.scm"
(lambda (port)
(write-csexp $exp port)))

🙄

@vbuaraujo

ah there's also

API rreferrence -> Input and Output -> Buffering

this is where call-with-output" is found

🙄

@catonano Huh, I never realized that the manual could be so confusing...

@catonano call-with-output-file takes the filename and a procedure of one argument, and calls the procedure with the open port.

(call-with-output-file "foo.txt"
(lambda (port)
(display "Hello, world!\n" port)))

with-output-to-file is similar but takes a procedure of zero arguments, and changes the default output port instead:

(with-output-to-file "foo.txt"
(lambda ()
(display "Hello, world!\n")))