File type

Represents a file in a file system (S_IFREG)

Direct parent types

Path
Represents file system path such as a file, a block device, etc
Direct subtypes: 7

Methods

access(f:File, mode:Int=0) Source: stdlib.ngs:6478
Check whether current process has the specified access to the file. Wrapper around ACCESS(2). Throws CException.

Parameters

modeOne of the file ACCESS::* modes, defaults to ACCESS::F_OK (test file existance only).

Returns

Bool

Example

access(File("mydata"), ACCESS::R_OK).not() throws Error("No access to data file").set("file", "mydata")
close(f:File) Source: stdlib.ngs:4590
Close a file and sets the "fd" field to null. Uses CLOSE(2). Throws InvalidArgument if file is not open. Throws FileIOFail if an underlying error occurs.

Returns

f
init(f:File, path:Str) Source: stdlib.ngs:4507
Create File object from the given path. The file is not open.

Example

f = File('/tmp/myfile')
init(f:File, fd:Int) Source: stdlib.ngs:4513
Create File object from the given file descriptor.
init(f:File, path:Path) Source: stdlib.ngs:4517
Undocumented
lines(f:File, cb:Fun) Source: stdlib.ngs:4553
Iterate over lines of the file

Parameters

fClosed File
cbfunction to call with successive lines from the file
lines(f:File) Source: stdlib.ngs:4568
Get all lines of the file

Returns

Arr of Str
lines(f:File, lines_:Arr) Source: stdlib.ngs:4573
Write given lines to the file. Overwrites the file.

Returns

Arr of Str
open(f:File, flags:Str) Source: stdlib.ngs:4531
Open a file and set the "fd" field to the file descriptor. Uses OPEN(2). Throws InvalidArgument if file is already open. Throws FileIOFail if an underlying error occurs.

Parameters

flagsCurrently "r", "w" or "a" for read/write/append

Returns

f
read(f:File) Source: stdlib.ngs:6498
Read whole file
Str(f:File) Source: stdlib.ngs:4521
String representation of File

Example

open(File("tmp/v8/LICENSE"), "r").Str().echo()  # Output: <File path=tmp/v8/LICENSE fd=4>
write(s:Str, f:File) Source: stdlib.ngs:6514
Write whole file if it's closed, write to file descriptor if it's open
write(f:File, s:Str) Source: stdlib.ngs:6519
Undocumented