With the introduction of file handling, a new datatype is added: the handle type hnd, which is used for files and devices.
Opening and reading a text file looks like this (working example):
Code: Select all
' SharpBASIC read text file
' -------------------------
option strict;
incl "lib/sys.sbi";
incl "lib/io.sbi";
decl sub readFile(const fileName: str);
main do
readFile("myfile.txt");
end
sub readFile(const fileName: str)
dim is
tf: hnd; ' file handle
fl: int; ' flag
tb: str; ' text buffer
end
do
' open file for input (read from text file)
tf = open(inp fileName);
' handle < 0 means error
if tf >= 0 do
loop do
while not eof(tf);
' read line
fl = read(tf, tb);
if fl == 0 do
print(tb);
else do
print("Error reading file " + fileName);
break;
end
end
close(tf);
else do
print("Error opening file " + fileName);
end
end