Skip to content

read0

Read text from a file or process handle

read0 f           read0[f]
read0 (f;o)       read0[(f;o)]
read0 (f;o;n)     read0[(f;o;n)]
read0 h           read0[h]
read0 (fifo;n)    read0[(fifo;n)]

Where

returns character data from the source as follows.

File symbol

Returns the lines of the file as a list of strings. read0 treats either line feed (LF) or carriage return plus line feed (CRLF) as the line delimiter, and removes the delimiters.

q)`:test.txt 0:("hello";"goodbye")  / write some text to a file
`:test.txt
q)read0`:test.txt
"hello"
"goodbye"

File symbol with offset and length

Returns the lines of the file, starting from byte o.

q)`:foo 0: enlist "hello world"
`:foo
q)read0 (`:foo;6)
"world"

Returns the lines in the n bytes of the file starting from byte o.

q)read0 (`:foo;6;2)
"wo"

The offset and length count bytes, not lines, so a line that crosses the end of the range is cut short.

q)read0 (`:test.txt;0;8)
"hello"
"go"

System or process handle

Returns a line of text from the source.

q)rl:{1">> ";read0 0}
q)rl`
>> xiskso
"xiskso"

Reading the console permits interactive input.

q)1">> ";a:read0 0
>> whatever
q)a[4+til 4]
"ever"

Fifo or named pipe

Where the argument is a handle fifo to a Fifo, returns the lines read from the pipe until end of file (EOF). Where the argument is (fifo;n), returns n characters from the pipe.

q)system"mkfifo mypipe"
q)system"(echo 'Red Hat Enterprise Linux' > mypipe &)"  / write to the pipe in the background
q)h:hopen`:fifo://mypipe
q)read0(h;8)
"Red Hat "
q)read0(h;8)
"Enterpri"
q)hclose h

Connection handles, File system, Interprocess communication

Q for Mortals §11.4.1 Reading and Writing Text Files