Libc.FILE for stdout

I need to ccall a function that expects a C FILE *. To have the function write to a file on disk, I figured out that I can get an appropriate FILE * using

foo = open("foo.txt", "w")

This works fine, but I’d also like to be able to have this work for stdout. So I tried:

ERROR: MethodError: no method matching fd(::Base.TTY)
Closest candidates are:
  fd(::Base.Filesystem.File) at filesystem.jl:231
  fd(::IOStream) at iostream.jl:36
 [1] Base.Libc.FILE(::Base.TTY) at ./libc.jl:87
 [2] top-level scope at none:0

I can instead do

Libc.FILE(RawFD(1), "w")

but is the fact that Libc.FILE(stdout) doesn’t work an oversight?


So in this method:

there are two issues for TTY:

  • fd(::TTY) is not defined; however Base._fd(::TTY) exists and appears to return a valid RawFD for stdout; so why don’t we just have Base.fd(io::TTY) = Base._fd(io)?
  • position(::TTY) and seek(::TTY) are not defined. Should there be an isseekable(io::IO) function to guard this?
1 Like

Same issue when using an IOBuffer