Connection handles

Kdb+ communicates with the system (console, stdout, stderr), file system, and other processes through connection handles.

There are three permanent system handles:

0  console
1  stdout
2  stderr

Other handles are created by hopen and destroyed by hclose.


A handle is an int atom but is variadic. Syntactically, it can be an int atom or a unary function.

q)1           / one is one
q)1["abc\n"]  / or stdout

A handle is an applicable value. It (and its negation) can be applied to an argument and iterated.

Write to a handle

Applied to an argument, a handle writes the argument to its target and returns itself.

stdout and stderr

q)a:1 "quick brown fox\n"
quick brown fox

Write only chars to stdout and stderr.

q)1[200 300]
  [0]  1[200 300]


Negate the handle to append a newline to the string.

q)1 "lazy dog"
lazy dog1
q)-1 "lazy dog"
lazy dog


Writing a string or a parse tree to the console evaluates it in the main thread.

q)0 "1 \"hello\""   /string

q)0 (+;2;2)         /parse tree

Reading from the console with read0 permits interactive input.


q)\ls data
ls: data: No such file or directory
  [0]  \ls data
q)h:hopen `:data/new
q)h                        /handle is an integer
q)type h                   /atom
q)h "now is the time"      /but can be applied as a unary
q)/and iterated
q)h each (" for all good men";" to come to the aid of the party")
3 3i
q)hclose h
q)read0 `:data/new         /hopen created file path
"now is the time for all good men to come to the aid of the party"

Read from a handle

Use read0, File Text, read1, or File Binary.

File system, Interprocess communication
hopen, hclose, hsym