Hi :wave:. How do I get the PDF or kindle version of HTDP book?
The Kindle edition can be obtained from Amazon at least :slightly_smiling_face:
Me again. Does anyone know if you are allowed to have an input and output port open on a file at the same time?
@mark.warren Yes. open-input-output-file
https://docs.racket-lang.org/reference/file-ports.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._open-input-output-file%29%29
(That returns two values (input and output port). So typically you’d use with define-values
to bind to two variables.)
@greg Argh, missed that, thanks, I’ve really got to learn to RTFM a bit more closely.
Well, it’s not in the Guide IIRC. Would need to look in the Reference under file ports. And although it’s certainly not a “weird” thing to do (read/write to the same file), it’s not something I’ve happened to do a lot, not happened to see in other code lately. So I kind of “knew” it was possible but had to check. :simple_smile:
I am wondering whether using open-input-file
and open-output-file
separately is better (unless it is a special port such as COM1)?
It does imply that it’s not a usual thing to do. It also implies that you can open separate input and output ports on the same file, maybe I should just give it a go and see.
Oh derp I didn’t read the docs carefully.
I think @soegaard2 is right.
Sorry.
Not a problem, all knowledge is good.
That’s probably the actual reason I’ve hardly ever seen open-input-output-file
in code. :simple_smile:
Wonder how it works. If I write to the output port, will the input port know that it needs to refresh its buffers?
Probably not.
As I say, probably best for me to try, and see what happens.
What’s the worst that can happen right :grin:
grabs popcorn
pauses Tour de France
Ok, I’m not that fast. Give me a mo.
@greg @soegaard2 Ok, initially the input port does not see the changes that the output port has made, so I probably need to do a refresh somehow, or reopen the input port, hmm.
FWIW it is also possible to disable buffering, but I don’t know how it affects throughput.
@soegaard2 Yep, that seems to work, as you say though, how it affects throughput will have to be seen.
Really quick test code #lang racket
(define out (open-output-file "../io.txt" #:exists 'can-update))
(define in (open-input-file "../io.txt"))
(file-stream-buffer-mode out 'none)
(write-string "test" out)
(displayln (read-line in))
(close-input-port in)
(close-output-port out)
If totally disabling buffering turns out to be too slow, you can also leave it on and flush-output-port
as necessary. (Of course now “as necessary” becomes a potential source of bugs.)
For certain kinds of ports like TCP you can’t disable buffering, so you need to flush manually. e.g. At the end of a request, before waiting to read back some response. That’s not such a tricky thing to get correct in most programs. It’s just a mistake everyone seems to make, at first. :simple_smile:
“why does my client get st…” “flush-output-port”
:smile:
Hmmm… Something I need to try later
More experimentation required