Fs.read_string¶
read_string :: fn (handle: File) (_0: string, _1: Error)
Read whole file content into string. Return new string instance containting file data and OK status on success, otherwise return empty string and error. Returned string is expected to be released by String.delete call if there was no error reported by function. Result string is zero terminated even if file is empty.
Example¶
#import "std/fs"
main :: fn () s32 {
// Open this file.
file, open_err :: Fs.open(#file, Fs.OpenMode.Read);
// Always check for errors.
if !is_ok(open_err) {
panic("Cannot open file with error: '%'!", open_err);
}
// Close file at the end of scope.
defer Fs.close(file);
// Read it's content.
content, read_err :: Fs.read_string(file);
// Check for errors.
if !is_ok(read_err) {
panic("Cannot read file with error: '%'!", read_err);
}
// Delete content string at the end of scope.
defer String.delete(content);
// Print file content to stdout.
print("%\n", content);
return 0;
}
Declared in: fs.bl