Skip to content

File system

File handles

A file handle is either

  • a symbol representing the filepath, e.g. `:/path/to/file
  • a system file handle:
    • 0 – console
    • 1 – stdout
    • 2 – stderr
  • an integer file handle returned by hopen

Relative filepaths are sought in the following locations, in order.

  1. current directory
  2. QHOME
  3. QLIC

Writing text to a file handle

Syntax: h x, h[x]

Where

  • h is a file handle or its negation
  • x is a string or parse tree

Writes a string to the file.

q)0 "1 \"hello\""
hello1

Negate the handle to append a newline to the string.

q)1 "String vector here\n"
String vector here
1
q)-1 "String vector here"    / equivalent
String vector here
-1

Writing a parse tree evaluates it in the main thread.

q)0 (+;2;2)
4

Reading from the console with read0 permits interactive input.

Not just system file handles.

q)a:hopen`:file.txt
q)a "first "
q)a "word\n"
q)hclose a

If h is negative and points to an existing file, then a newline is included.

q)a:hopen`:file.txt
q)neg[a] "first line"
q)neg[a] "second line"
q)hclose a

Prepare Text

The Prepare Text operator converts a table to strings ready to write to file.

Keyword csv specifies the comma delimiter to be used when writing or loading a CSV.

File functions

Three file operators and 14 keywords read from and write to file descriptors.

A file descriptor is either

  • a file handle
  • a list (file handle; offset; length) to specify that the file is to be read from offset (int atom) for length (int atom) characters.
function semantics
0: File Text:
- Prepare Text
- Save Text
- Load CSV
- Load Fixed
- Key-value Pairs
1: File Binary:
- Read Binary
- Save Binary
2: Dynamic Load of C shared objects
dsave Save global tables to disk
Enum Extend Extend and load sym file
get Read or memory-map a kdb+ data file
hclose Close a file or process handle
hcount File size
hdel Delete a file or folder
hopen Open a file or process handle
hsym Convert a symbol to a file handle
load Load binary data from the filesystem
read0 Read text from a file
read1 read bytes from a file or named pipe
rload Load a splayed table
rsave Save a splayed table to a directory
save Save global data to file
set Assign a value to a variable or file