Skip to content

ReadFileAsText()

ts
ReadFileAsText(filePath: string, length?: number, offset?: number, whence?: number): respReadString;

Reads the specified file as plain text from the VFS and returns it in a respReadString response object.

WARNING

If the file is not a plain-text file, the Data() method of the resulting object will contain unusable data. Use this method with caution.

Parameters

ParameterTypeRequirementExplanation
filePathstringrequiredThe fully qualified, root-based, POSIX path to the file in the VFS
lengthnumberoptionalRead at most this many bytes (plain-text characters) starting from offset (or until EOF); if not specified or zero, a default buffer of 32 KiB is used
offsetnumberoptionalSkip this many bytes from the beginning of the file; if not specified, reads from the beginning
whencenumberoptionalOne of: FromStart, FromEnd; if not specified, reads from the beginning

Example

ts
var vfs = GetCurrentVFS();
if (vfs != null) {
    var resp = vfs.ReadFileAsText("/data/welcome.txt", 1024, 6, FromStart);
    if (resp.Ok()) {
        Log(resp.Data()); // Will log: World!
    } else {
        Log("Error: " + resp.ErrorMsg());
    }
} else {
    Log("VFS was null");
}