A simple way to read a Unix text file f is to enter
mûsys.readmat{f}
where f is a character vector giving a path name. The file is read
into m as a matrix, in which all rows have been made equal in
length to the longest row in the file, by appended blanks.
A file can be read as a vector, with newline characters embedded, by a
Pipe In command like
$<FileVarName cat FileName
or by a function such as:
read{file}:{
if (0>sûsys.filesize{file}) ÙÂ'read failed: ',îs;
aûsÒ' ';
fdûsys.open{file;`O_RDONLY;0};
sys.read{fd;a;s};
sys.close{fd};
a
}
Partition Count and
Partition (Ú) can make the
result of such a vector into a nested array of lines.
In this function the local variable s tells
sys.read how many characters to read. Clearly, with minor
alterations to the code shown, the file can also be read a portion at a time. Moreover,
the function sys.lseek can be used to choose a point from which to
start reading a portion of the file.
Data can be written to a Unix text file by Pipe Out
and Pipe Out Append commands like
$>FileVarName FileName
$>>FileVarName FileName
when the data is a character vector with embedded newlines, or by functions
like the following, which also convert any character matrix arguments to vectors, deleting
trailing blanks and appending newline characters as required:
write{file;data}:{
if ((`char=©data)^2=ÒÒdata) dataûclean{data};
if (0<fdûsys.open{file;
`O_CREAT`O_TRUNC`O_WRONLY;
8Â6 4 4})
{
sys.write{fd;data;#data};
sys.close{fd};
};
}
clean{n}:Ø(cleanline¡<@1 n),¡<"\n"
cleanline{x}:(-+/^\' '=÷x)Õx
You can easily devise variations for yourself, for both reading and writing.